sed & awk help...

I have a question. Take the following statement

awk -F\| '{print $21}' testfile | sed 's/\//\\/g' > newfile

This will grab the 21st column of a | delimited text file, replace the forward slashes "/" , with back slashes "\", and redirect the output newfile. Now, how do I get the output reinserted back into the 21st column of the original file?

Shimb0

Use "cut" to create a file with columns 1 through 20. Use it again to create a file with columns 22 on. Use "paste" to connect all three files.

OK, I have the 3 separate files, I'm just confused on how to use paste to paste them back together. The man page on paste is confusing. Got any examples?

I'll illustrate by example:

$ cat a
This is line one of a
This is line two of a
This is line three of a
This is line four of a
$ cat b
This is line one of b
This is line two of b
This is line three of b
This is line four of b
$ cat c
This is line one of c
This is line two of c
This is line three of c
This is line four of c
$ paste -d '|' a b c > d
$ cat d
This is line one of a|This is line one of b|This is line one of c
This is line two of a|This is line two of b|This is line two of c
This is line three of a|This is line three of b|This is line three of c
This is line four of a|This is line four of b|This is line four of c

HTH

Hey Shimb0,
As an alternative, you could just use modified awk statement from what you have currently. The below is an example:

awk -F'|' '\
{
gsub(/\//, "\\", $21)
print $0
}' OFS='|'

Make sure the version of awk you are using supports gsub. On Sun, you will have to use the one in /usr/xpg4/bin. I don't know about other platforms.

The -F sets the field separator to |. gsub replaces / with \ in field 21 only. print $0 will print the entire record with the modified field 21, which is what you want I think. OFS will set the field separator in your output to | as well.

You may have to direct this output to a temp file and then mv the temp file over the original, like this:

awk -F'|' '\
{
gsub(/\//, "\\", $21)
print $0
}' OFS='|' original.txt > tmp.txt
mv tmp.txt original.txt
rm tmp.txt

Hope this helps,
TioTony