Find and Replace in multiple fields using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and Replace in multiple fields using awk
# 1  
Old 02-24-2012
Find and Replace in multiple fields using awk

Hi,

Say I have a record "1|22| | |". In which the third and fourth fields are <space> alone. I have to replace the <Space> with <null>.

Input:
Code:
"1|22| | |" --> "1|22|<space> |<space> |"

Expected output:
Code:
"1|22|||" --> "1|22|<null> |<null>|"

I tried:
Code:
echo "1|22| | |" | awk -F '|' -v v1=" " -v v2="" 'BEGIN{OFS="|";}{gsub(v1,v2,$3);print}' 
echo "1|22| | |" | awk -F '|' -v v1=" " -v v2="" 'BEGIN{OFS="|";} {gsub(v1,v2,$4);print}'

The above code works fine. But is there a way in which I can pass a array of fields rather than one by one?? Smilie
# 2  
Old 02-24-2012
You can do a loop, and use $ to turn a field number into field contents:

Code:
for(N=1; N<=NF; N++) sub(/ /, "", $N);

...but I suspect you're overthinking this a touch -- why bother using fields at all? Do a search/replace on the line itself and you'll get it all done in one gsub:

Code:
awk '{ gsub(/[|] [|]/, "||") } 1' inputfile

---------- Post updated at 10:04 AM ---------- Previous update was at 10:01 AM ----------

Okay, that doesn't work because gsub refuses to overlap like that. But my first example still works when you set FS and OFS properly.

The regex might as well be /^ $/ for that case, to prevent it eating spaces in the middle of a string.
# 3  
Old 02-24-2012
I just want to do on few fields alone. If othert fields have space alone, that wont be a problem...I have to do a search and replace on selective fields alone
# 4  
Old 02-24-2012
The "3|4" is the list of fields you want to replace here. It's split() into the array F so that F[1]=3, F[2]=4, using the same separator as everything else... Then we loop over that array, for(N in F) which should do two loops with N=1 and N=2 respectively. We look up the field in F, convert it to field contents with $, and substitute on it.

I think just plain sub() should be fine here, since you don't need to do more than one substitution per token.
Code:
awk -v FS="|" -v OFS="|" 'BEGIN { split("3|4", F); } { for(N in F) sub(/^ $/, "", $(F[N])) } 1' filename

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 02-24-2012
What about something like?

Code:
$ echo "1|2| | |5" | awk 'BEGIN {FS=IFS=OFS="|"} {if ($3==" ") {$3=""} ; if ($4==" ") {$4=""} ; print $0}'
1|2|||5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print multiple fields with awk

so its common knowledge one can print multiple fields with simple commands like this: echo 12 44 45 552 24 | awk '{print $1,$4,$3}' but suppose i want to avoid specifying the "$" symbol. is that possible? can something like this be done: echo 12 44 45 552 24 | awk '{print $(1,4,3)}' ... (9 Replies)
Discussion started by: SkySmart
9 Replies

2. Shell Programming and Scripting

Replace 0 with 1 in multiple fields with awk

Hello, I have the following input file: 1 3 3 2 3 3 4 0 4 0 5 4 5 2 2 0 5 3 4 0 6 0 3 2 I am trying to remove all zeroes in fields 2 and 4 and replace them with "1's" I tried the following, but it's not working awk -F"\t" '{ if (($2==0) || ($4==0) $2=1; $4=1; print $0 ) }' input ... (8 Replies)
Discussion started by: Rabu
8 Replies

3. Shell Programming and Scripting

Find fields and replace using awk

Code: Using ksh Var1=`awk -F";" {print $1}' Input2.txt` cat Input1.txt | awk -F";" '{$3="Var1"}' > Output.txt (13 Replies)
Discussion started by: Roozo
13 Replies

4. Shell Programming and Scripting

awk gsub multiple fields

Hi, I am trying to execute this line awk -F ";" -v OFS=";" '{gsub(/\./,",",$6); print}' FILE but for multiple fields $6 $7 $8 Do you have a suggstion? Tried: awk -F ";" -v OFS="";"" "function GSUB( F ) {gsub(/\./,\",\",$F); print} { GSUB( 6 ); GSUB( 7 ); GSUB( 8 ) } 1"... (2 Replies)
Discussion started by: nakaedu
2 Replies

5. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

6. Shell Programming and Scripting

AWK: merge two files and replace some fields

Need some code tweak: awk 'END { for (i=1; i<=n; i++) if (f2]) print f2] } NR == FNR { f2 = $1] = $0 next } $1 in f2 { delete f2 }1' FS=, OFS=, 2.csv 1.csv > 3.csvfile 1.csv have: $1,$2,$3,$4,$5,$6,$7,$8,$9...... file 2.csv have: $1,$2,$3,$4,$5,$6 (2 Replies)
Discussion started by: u10
2 Replies

7. Shell Programming and Scripting

help with search and replace in multiple fields

I have a pipe delimited file with 27 fields. Each record has 26 fields. I need to search for the 25,26,27 fields and replace "," with nothing. How can I acheive this. Sed is more preferred. e.g data row o/p (5 Replies)
Discussion started by: dsravan
5 Replies

8. Shell Programming and Scripting

AWK multiple fields separators

I need to print the second field of a file, taking spaces, tab and = as field separators. ; for 16-bit app support MAPI=1 CMC=1 CMCDLLNAME32=mapi32.dll CMCDLLNAME=mapi.dll MAPIX=1 MAPIXVER=1.0.0.1 OLEMessaging=1 asf=MPEGVideo asx=MPEGVideo ivf=MPEGVideo m3u=MPEGVideo (2 Replies)
Discussion started by: PamPam
2 Replies

9. Shell Programming and Scripting

awk find and replace in multiple files

Hi I use the following code to replace ‘.' with ‘N' in my files and keep both versions. awk '{ gsub(/\./,"N"); print }' file_0001.txt > path/to/new/dir/file_0001.txt I need help on how to apply the code to 100 files instead of doing them one file at a time. The files are labeled... (7 Replies)
Discussion started by: jdhahbi
7 Replies

10. UNIX for Dummies Questions & Answers

How do I read/find/replace fields in a csv datafile?

hello. I'm somewhat a novice here so please be patient. My stumbling block when loading csvs into ORACLE tables is this: I need to read a csv datafile, check several fields in each line, and if any of stated fields contain A ZERO only then replace it with a null/blank character. I had a... (9 Replies)
Discussion started by: MrCarter
9 Replies
Login or Register to Ask a Question