IBM Support

How to Change Line Using SED Command

Question & Answer


Question

This document explains how to change the only second line in a file to a string of characters using the SED command.

Answer


Environment

AIX 5L To substitute a particular line in a file with a different string of characters, use the following format:
# sed "[linenumber]s/pattern/replacement/flags" filename 

Where

linenumber - line number that you want to replace in the file

pattern - string of characters you want to change on linenumber

replacement - string of characters that will replace the pattern

flags - number of occurrences (in this case, you are only changing the first occurrence)

The following example uses a file called filename. You will replace the second line "5678" with a string of ones, "1111".

  1. Display the file you want to change.
    # cat filename
    1234
    5678
    9123
    4567
    

  2. Change line 2 to your new string of characters. This example is using "1111".
    # sed "2s/5678/1111/1" filename
    1234
    1111
    9123
    4567
    

Related Documentation

For more in-depth coverage of this subject, refer to the Software Documentation Library, which can be accessed online:

AIX Version 5L:
http://publib16.boulder.ibm.com/pseries/

[{"Product":{"code":"SWG10","label":"AIX"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Component":"File management","Platform":[{"code":"PF002","label":"AIX"}],"Version":"5.3;5.2;5.1","Edition":"","Line of Business":{"code":"LOB08","label":"Cognitive Systems"}}]

Historical Number

isg1pTechnote1196

Document Information

More support for:
AIX

Software version:
5.3, 5.2, 5.1

Operating system(s):
AIX

Document number:
669577

Modified date:
17 June 2018

UID

isg3T1000495

Manage My Notification Subscriptions