Replace text of a specific line number using sed

7,741

Solution 1

Commands in sed accept an address range. In this case, to limit the replacement to just the first line, the range should be the single address 1 i.e.

sed '1 s/AccountSettings/Users/' users.html

Solution 2

Perl

$ perl -pi -e 's/AccountSettings/Users/ if $. == 1' input.txt
  • -pi allows to make changes for input file in-place, i.e. alter the actual file. If you want to see result before overwriting the file, use -p instead
  • s/AccountSettings/Users/ perform first substitution for pattern.
  • $. is processed line count

AWK

$ awk 'NR==1{sub("AccountSettings","Users")};1' input.txt                
<template name="Users">
AccountSettings
  • NR==1{sub("AccountSettings","Users")} is Pattern or Condition {actions} structure typical of awk. Action is performed when condition is true.
  • NR==1{} is number of processed lines, specifically we're looking if number of lines is equal to 1.
  • sub("AccountSettings","Users") performs replacement for the first match of the pattern.
  • 1 is pattern with omitted action. Default action for pattern that is true is to print, and making pattern to be 1 ensures we print resulting lines.

Python

One liner with reading stdin:

$ python -c 'import sys;print("\n".join([ l.strip().replace("AccountSettings","Users") if i==0 else l.strip()  for i,l in enumerate(sys.stdin)]))' < input.txt

Script version:

#!/usr/bin/env python
from sys import argv;
with open(argv[1]) as f:
    for index,line in enumerate(f):
        text = line.strip().replace("AccountSettings","User") if index == 0 else line.strip()
        print(text)
  • core idea is to either redirect stdin stream and read lines from there or provide file as argv[1] - first command-line argument to script
  • We use variable = value_1 if condition else value_2 structure to build up a list of lines with appropriate values in one-liner, while in script version assignment to text variable and immediate printing of that variable is performed. The condition for the replacement is of course line number

Ruby

$ ruby -pe '$_=$_.sub(/AccountSettings/,"Users") if $. == 1' input.txt                                                                             
<template name="Users">
AccountSettings
  • Similar idea to Perl, -p switch allows us to print $_ (current line) variable each time.
  • $_=$_.sub(/AccountSettings/,"Users") if $. == 1 is action if condition structure,where we use .sub() function to perform pattern replacement and assign it back to $_ buffer if line number is 1

Solution 3

Use following command:

sed   's/name="AccountSettings"/name="Users"/g' users.html
Share:
7,741

Related videos on Youtube

Le Qs
Author by

Le Qs

Updated on September 18, 2022

Comments

  • Le Qs
    Le Qs over 1 year

    I have a html file users.html that i want to replace the first line. The first line looks like this

    <template name="AccountSettings">
    

    I want the first line to read

    <template name="Users">
    
    • fiatux
      fiatux over 7 years
      It is prudent to use an XML parser to parse XML.
  • Ravexina
    Ravexina almost 7 years
    Nice answer ...