Sed Replace In-Line a Specific Column Number Value at a Specific Line Number

sed replace in-line a specific column number value at a specific line number

Here is one way:

$ sed '/^username4/{s/ [^ ]*/ anything/3}' file
username1 20130310 enabled 20130310 enabled
username2 20130310 enabled 20130321 disabled
username3 20130320 disabled 20130321 enabled
username4 20130310 disabled anything disabled

# store changes back to the file
$ sed -i '/^username4/{s/ [^ ]*/ anything/3}' file

But avoiding awk because sed has the -i option isn't a good reason. awk is more suited to working with this kind of problem.

$ awk '$1=="username4"{$4="anything"}1' file
username1 20130310 enabled 20130310 enabled
username2 20130310 enabled 20130321 disabled
username3 20130320 disabled 20130321 enabled
username4 20130310 disabled anything disabled

# store changes back to the file
$ awk '$1=="username4"{$4="anything"}1' file > tmp && mv tmp file

With awk you can easily do field comparison and editing, using shell variable isn't a quoting nightmare and understanding scripts you wrote only yesterday isn't and issue unlike with sed:

$ linenumber=4

$ newvalue=anything

$ awk 'NR==n{$4=a}1' n=$linenumber a=$newvalue file
username1 20130310 enabled 20130310 enabled
username2 20130310 enabled 20130321 disabled
username3 20130320 disabled 20130321 enabled
username4 20130310 disabled anything disabled

$ awk 'NR==n{$4=a}1' n=$linenumber a=$newvalue file > tmp && mv tmp file

sed search and replace specific column only

I would use awk for this:

$ awk 'BEGIN{split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",a)} {$2=a[$2+0]}1' a
>2013 Aug 02 23 37 00 73.3
>2013 Aug 02 23 42 00 73.4
>2013 Aug 02 23 45 00 73.3
>2013 Aug 02 23 47 00 73.1
>2013 Aug 02 23 52 00 73.1
>2013 Aug 02 23 57 00 73.1

To update the field with the new content, just redirect and then move:

awk .... file > temp_file && mv temp_file file

Explanation

What we do is to give awk a list of strings with the months names. Once we convert it into an array, a[1] will be Jan, a[2] Feb and so on. So then it is just a matter of replacing the 2nd field with a[2nd field].

  • BEGIN{split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",a)} fetches the data and inserts into the a[] array.
  • {$2=a[$2+0]} sets the 2nd field as a[2nd field]. The $2+0 is done to convert 08 to 8.
  • Finally 1 evaluates as true and makes awk perform its default action: {print $0}.

Sed replace pattern with line number

Simple awk oneliner:

awk '{gsub("###",NR,$0);print}'

Replace pattern in specific column in sed

You may just process the last column with sed:

sed -E 's/[^ ]*_([^ ]*) *$/\1/' file

The output:

BB_12  AA
BB_13 AB
BB_14 AD
BB_15 AC

Awk alternative:

awk '{ sub(/^[^ ]+_/, "", $2) }1' OFS='\t' file

replace specific columns on lines not starting with specific character in a text file

With GNU awk, set i/o field separators to empty string so that each character becomes a field, and you can easily update them.

awk -v cols='2 4 7' '
BEGIN {
split(cols,f)
FS=OFS=""
}
!/^>/ {
for (i in f)
$(f[i])="N"
}
1' file

Also see Save modifications in place with awk.

Bash - how to replace line specific field seperated by colon delimiter (:)?

Using awk you can use field separator : and target specific row and column:

awk -F: 'NR==4{$1="anything for dummies"}1' OFS=: file
Java for dummies:Pauline:10.56:4:3
Bash for dummies:David:10.45:4:5
PHP for dummies:Frederich:10.67:4:4
anything for dummies:Pearlyn:10.56:4:5
C++ for dummies:Jared:10.46:4:5


Related Topics



Leave a reply



Submit