Reputation: 5735
I have a string containing this (field separator is the percentage sign), stored in a variable called data
201%jkhjfhn%kfhngjm%mkdfhgjdfg%mkdfhgjdfhg%mkdhfgjdhfg%kdfhgjgh%kdfgjhgfh%mkfgnhmkgfnh%k,gnhjkgfn%jkdfhngjdfng
I'm trying to print out that string replacing the percentage sign with a pipe but it seems harden than i thought:
echo ${data} | awk -F"%" 'BEGIN {OFS="|"} {print $0}'
I know I'm very close to it just not close enough.
I see that code as:
1 echo the variable value into a awk session
2 set field separator as "%"
3 set as output field separator "|"
4 print the line
Upvotes: 2
Views: 560
Reputation: 184995
Try this :
echo "$data" | awk -F"%" 'BEGIN {OFS="|"} {$1=$1; print $0}'
From awk manual
Finally, there are times when it is convenient to force awk to rebuild the entire record, using the current value of the fields and OFS. To do this, use the seemingly innocuous assignment:
$1 = $1 # force record to be reconstituted print $0 # or whatever else with $0
Another lightweight way using only tr
if you search an alternative for awk
:
tr '%' '|' <<< "$data"
Upvotes: 3
Reputation: 206679
Sputnick gave you the awk
solution, but you don't actually need awk
at all, just use your shell:
echo ${data//%/|}
Upvotes: 3