awk to delete fields from flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to delete fields from flat file
# 8  
Old 11-05-2013
Quote:
Originally Posted by Aditya_001
Hi Franklin, the above command is not removing the 16th feild which starts with 354 line in a file.

I have executed the above command on my unix box and this is how it shows,

Code:
 
XXXXX:XXXXXXXX [XXXXXXXXXXXX] $ awk -F"|" '/^354/{NF=NF-2;NF++}1' OFS="|" D171_test_file
ZHV|3011|D0171001|L|LPN|Z|PAN|20131104000148||||OPER|
748|49790|
354|902|20131029|1|2|3|4|5|6|7|8|9|10|11|12||
352|5759991|SP29|1200031516735|20131101|
354|||||FLAT 2 AT||2||GARENDON ROAD|||MORDEN|SURREY|SM4 6LN|ADI0001|
748|49792|
352|5760019|SP27|1200021998833|20131101|
XXXXX:XXXXXXXX [XXXXXXXXXXXX] $
 
 
It should have removed the 16th feild from the file where line starts with 354
 
354|902|20131029|1|2|3|4|5|6|7|8|9|10|11|12|

354|||||FLAT 2 AT||2||GARENDON ROAD|||MORDEN|SURREY|SM4 6LN|

Thanks very much,

That's what I get get:

Code:
$ cat file
ZHV|3011|D0171001|L|LPN|Z|PAN|20131104000148||||OPER|
748|49790|
354|902|20131029|1|2|3|4|5|6|7|8|9|10|11|12||
352|5759991|SP29|1200031516735|20131101|
354|||||FLAT 2 AT||2||GARENDON ROAD|||MORDEN|SURREY|SM4 6LN|ADI0001|
748|49792|
352|5760019|SP27|1200021998833|20131101|
$
$
$ awk -F"|" '/^354/{NF=NF-2;NF++}1' OFS="|" file
ZHV|3011|D0171001|L|LPN|Z|PAN|20131104000148||||OPER|
748|49790|
354|902|20131029|1|2|3|4|5|6|7|8|9|10|11|12|
352|5759991|SP29|1200031516735|20131101|
354|||||FLAT 2 AT||2||GARENDON ROAD|||MORDEN|SURREY|SM4 6LN|
748|49792|
352|5760019|SP27|1200021998833|20131101|
$

# 9  
Old 11-05-2013
@Franklin52 it works nicely..

@aditya_001 whether you are reading post ?
This User Gave Thanks to Akshay Hegde For This Post:
# 10  
Old 11-05-2013
Thanks very much for your help Akshay, I really appreciate your clear cut explanation,

A quick doubt, When I am trying to divert the output of the below command to temp file its actually adding |^H| character at the end of the 16th feild,

If I hard-code the filename then it produces the output as expected, any idea why diversion is adding this specical character ??


Code:
 
awk -F"|" '/354/{$16="\b"}1' OFS="|" $1 > temp
mv temp $1

Thanks very much,
# 11  
Old 11-05-2013
This is what Akshay Hegde added to "remove" (better: hide) the last pipe: a <backspace> char.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Flat File Conversion Script

In awk how would I flatten input.txt to output.txt? Input: givenname: Darth sn: Vadar mail: d.vadar@deathstar.com uid: dv12345 orclguid: 1234567890 givenname: Carlito sn: Brigante mail: c.brigante@paradise.com uid: cb12345 orclguid: 2134567890 Output: ... (3 Replies)
Discussion started by: u20sr
3 Replies

2. Shell Programming and Scripting

awk to place value at 24 field in a flat file issue

I am trying to add 0393 value at 24th feild using the below command, but its adding at all the lines including header and trailer Input file: ZHV|2657|D0217001|T|TXU|Z|PAN|20131112000552||||OPER| 754|52479| 492|489|SP40|1014570286334|20131111|20131201|14355334|CHAMELON... (1 Reply)
Discussion started by: Aditya_001
1 Replies

3. Shell Programming and Scripting

Delete last 2 fields from every record in a file

Sample file record : "20130617003","2013-06-18T07:00:03","OUTWARD","01001011","TEST PLC","","HFX834346364364","20130617","10","DUM87534758","","1.28","826","020201","65879278","","","","","","010101","56789","DUMMY... (3 Replies)
Discussion started by: bigbuk
3 Replies

4. UNIX for Dummies Questions & Answers

using sed delete a line from csv file based on specific data in two separate fields

Hello, :wall: I have a 12 column csv file. I wish to delete the entire line if column 7 = hello and column 12 = goodbye. I have tried everything that I can find in all of my ref books. I know this does not work /^*,*,*,*,*,*,"hello",*,*,*,*,"goodbye"/d Any ideas? Thanks Please... (2 Replies)
Discussion started by: Chris Eagleson
2 Replies

5. Shell Programming and Scripting

SED | Awk flat file one liner

sed awk one liner (2 Replies)
Discussion started by: jap2614
2 Replies

6. Shell Programming and Scripting

use awk to ssh from variable in flat file

flat file looks like ooss-pfgg-1234,vol_name_1, mail-list decoded = hostname,volum_name,mail_list each line has diff info am trying to ssh into each fist field, check vol usage for second field, and if greater than 90% send mail to mail-list got the second and third part working, ... (1 Reply)
Discussion started by: riegersteve
1 Replies

7. Shell Programming and Scripting

awk to compare flat files and print output to another file

Hello, I am strugling from quite a some time to compare flat files with over 1 million records could anyone please help me. I want to compare two pipe delimited flat files, file1 with file2 and output the unmatched rows from file2 in file3 Sample File1: ... (9 Replies)
Discussion started by: suhaeb
9 Replies

8. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

9. Shell Programming and Scripting

Search flat file and return 3 fields

I need to be able to search a flat file (comma-separated values) for a specific value and then return the following 2 fields into variables. Here's a sample flat file: SN,Account,IPaddress W120394YF,adam,10.0.20.2 W394830PR,betty,10.0.20.3 W847582TD,charlie,10.0.20.4... (7 Replies)
Discussion started by: da2357
7 Replies

10. Shell Programming and Scripting

Awk to convert a flat file to CSV file

Hi , I have a file with contents as below: Contract Cancellation Report UARCNCL LOS CODE DATE REAS TYPE AMOUNT AMOUNT LETTER BY ========= ======= ==== ==== ==== ========= ==== ==== 8174739 7641509 1S NONE CRCD 30-JUN-2008 NPAR N .00 .00 CCAN 8678696 8091709 1S NONE DDEB 30-JUN-2008... (14 Replies)
Discussion started by: rkumudha
14 Replies
Login or Register to Ask a Question