Nandy
May 18, 2014, 12:11pm
1
Input file :
12345|12345|12345|12345
12345|12345|12|13|12345
12345|12345|1|234|12345
12345|12345|12345|12345
Output :
12345|12345|12345|12345
12345|12345|12 13|12345
12345|12345|1 234|12345
12345|12345|12345|12345
Change is : If the string 13 to 17 contains any | symbol then it needs to change it to " " (SPACE). I have tried the below and confused.
cat testfile | awk ' { sub(/\|/," ",substr($0,13,17) ; print $0 } '
Error : The third parameter in the sub is unchangeable.
Could anyone please advise , how to change this using awk or something..
Your advise would be great help for me.
Thanks,
Nanthu.
Yoda
May 18, 2014, 1:08pm
2
So the fields that does not require substitution (pipe to blank space) will have length >= 5?
Nandy
May 18, 2014, 3:13pm
4
Hi,
The data from position 25 to 30 should not contain | as | is the delimiter. Hence the if the position 25 to 30 contain |, we just want to replace it with SPACE.
I hope that I am clear.
Regards,
Nandy.
Hi, try:
awk '{print $1,$2,(NF>4)?$3" "$4:$3,$NF}' FS=\| OFS=\| file
----
bobby_2000:
Hi, ...
Please start a new thread for this
Nandy
May 18, 2014, 4:01pm
6
Hi,
Agree but I do not want to print it rather I want to get update in the file. I meant file content should be changed as desired.
Please assist and Thanks indeed for your quick responses.
Regards,
Nandy.
Try redirecting it to a new file
awk '{print $1,$2,(NF>4)?$3" "$4:$3,$NF}' FS=\| OFS=\| file > newfile
--
and if successful you could try replacing the old file with the modified version...
awk '{print $1,$2,(NF>4)?$3" "$4:$3,$NF}' FS=\| OFS=\| file > newfile && mv newfile file
awk '{FIELDWIDTHS = "12 5 100"} {gsub(/\|/, " ", $2)}1' file > file.tmp && mv file.tmp file
---------- Post updated at 02:28 AM ---------- Previous update was at 02:26 AM ----------
for 25 to 30,
awk '{FIELDWIDTHS = "24 6 100"} {gsub(/\|/, " ", $2)}1' file > file.tmp && mv file.tmp file
Nandy
May 19, 2014, 9:55am
9
Hi,
Just need clarification here,
What is the purpose mentioning 1 after gsub {gsub(/\|/, " ", $2)}1'
Cant i do without FIELDWIDTH because i have more than 190 columns in my datafile.
Cant i use substr as target in gsub/sub function gsub(/\|/," ",substr($0,13,17))
---------- Post updated at 08:55 AM ---------- Previous update was at 04:46 AM ----------
Hi,
I am not getting the result, please look at what i tried.
EMP|IX28|Mordasini |Luca |IX|
EMP|IX29|NAN|THA |J|rgen |IX|
EMP|IX30|Ke|s |Peter |IX|
EMP|IX31|Mayer |Bernard |IX|
EMP|IX32|Ohnacker |Martin |IX|
EMP|IX33|Pankratz |Ewald |IX|
EMP|IX34|Seitz |Matthias |IX|
EMP|IX35|Straub |Michael |IX|
EMP|IX36|von Alkier|Jacqueline|IX|
EMP|IX37|Wu |Qin Jane |IX|
The above highlighted data | symbol should be remvoed. Hence i have issued,
cat TESTFILE2 | awk ' BEGIN { FIELDWIDTHS = "4 5 11 11 3"} { gsub(/\|/," ",$3) }1' FS=\| OFS=\|> TESTFILE3
But output comes adjusted
EMP|IX28|Mordasini |Luca IX
EMP|IX29|NAN|THA |J|rgen IX
EMP|IX30|Ke|s |Peter IX
EMP|IX31|Mayer |Bernard IX
EMP|IX32|Ohnacker |Martin IX
EMP|IX33|Pankratz |Ewald IX
EMP|IX34|Seitz |Matthias IX
EMP|IX35|Straub |Michael IX
EMP|IX36|von Alkier|Jacqueline|IX|
EMP|IX37|Wu |Qin Jane |IX|
Questions :
$3 should represent 3rd field ?
There are pipe as column delimeter but someother | is replacing.
Please assist
Regards,
Nandy
Subbeh
May 19, 2014, 11:08am
10
Give this a try:
awk '{x=substr($0,10,10); gsub(/\|/," ",x); print substr($0,0,10), x, substr($0,20)}' OFS="" file