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 thea[]
array.{$2=a[$2+0]}
sets the 2nd field asa[2nd field]
. The$2+0
is done to convert08
to8
.- Finally
1
evaluates as true and makesawk
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
Setting Up a Cronjob on Google Compute Engine
Convert an Iso Date to Seconds Since Epoch in Linux Bash
Linux How to Copy But Not Overwrite
How to Increase the Scrollback Buffer in a Running Screen Session
Assigning Dynamic Bash Variable Names Using a for Loop Seq
What's the Difference Between ./Script.Sh and Bash Script.Sh
Variables Set in a Bash 'While Read' Loop Are Unset After It
Get a Substring from a File Shell Script
Write-Only Mapping a O_Wronly Opened File Supposed to Work
Platform Independent Resource Management
What Is The Significance of This_Module in Linux Kernel Module Drivers
Read a File and Split Each Line into Multiple Variables
A Running Bash Script Is Hung Somewhere. How to Find Out What Line It Is On