Delete specific line in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete specific line in awk
# 1  
Old 12-09-2011
Delete specific line in awk

Hi, I'm totally new to awk, so thanks in advance for your help.

I want to make a csv file out of a text file I've exported from a database I want to do two things:
1. Change pipes ("|") to commas (",") to make a text file into CSV
2. Remove the second line of the file, which is useless junk.

Here's what I have so far to accomplish #1. I'm struggling on #2. Help?
Code:
function pull() {
cat $@ | awk '{gsub(/\|/,","); print;}' > $@.csv;
}

Many thanks

Last edited by Franklin52; 12-10-2011 at 02:00 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 12-09-2011
Hi,
maybe sed is a better reply to that something like
Code:
sed "s/|/,/g; 2d" $@ >$@.csv

This User Gave Thanks to Chirel For This Post:
# 3  
Old 12-09-2011
Quote:
Originally Posted by pip56789
function pull() {
cat $@ | awk '{gsub(/\|/,","); print;}' > $@.csv;
}
You get a useless use of cat award Smilie

You don't need gsub to handle separators in awk. awk handles separators natively, via the FS and OFS variables. Just make sure you alter the line before printing it -- even something useless like $1=$1 will do -- and it'll change all the separators for you.

Also, pretty sure that redirection won't do what you want it to -- replace in every file. You can't read and write the same file at the same time with shell redirection. You also can't redirect to several output files at once with $@.

How about this:

Code:
awk -v FS="|" -v OFS="," '{ $1=$1 } NR != 2' < input > output

...which, to overwrite the original file, you'd do:

Code:
awk -v FS="|" -v OFS="," '{ $1=$1 } NR != 2' < input > /tmp/$$
cat /tmp/$$ > input.converted

Remove the '.converted' once you've tested the code and found it does what you want -- it really stinks to lose all your originals to a bad program.

If you wanted to convert many files:

Code:
function fixfiles
{
        for FILE in "$@"
        do
                awk -v FS="|" -v OFS="," '{ $1=$1 } NR != 2' <"$FILE" > /tmp/$$
                cat /tmp/$$ > "$FILE".converted
        done
}

Many thanks[/QUOTE]
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 12-12-2011
Thank you, both. Very helpful. Glad you guys are on this board!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Delete lines above and below specific line of text

I'm trying to remove a specific number of lines, above and below a specific line of text, highlighted in red: <STMTTRN> <TRNTYPE>CREDIT <DTPOSTED>20151205000001 <TRNAMT>10 <FITID>667800001 <CHECKNUM>667800001 <MEMO>BALANCE </STMTTRN> <STMTTRN> <TRNTYPE>DEBIT <DTPOSTED>20151207000001... (8 Replies)
Discussion started by: bomsom
8 Replies

3. Shell Programming and Scripting

Delete a specific line containing non consecutive number?

Dear Specialists, I have following data 1 1 2 2 2 3 3 3 6 4 3 4 5 4 9 6 5 11 7 6 7 and I would like to obtain data like below 1 1 2 2 2 3 4 3 4 7 6 7 (2 Replies)
Discussion started by: Ryan Kim
2 Replies

4. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

5. Shell Programming and Scripting

Delete a specific line from the feed file

Hi All, I have came across an issue where I will grep for a primary key and then I have to delete that particular line from the feed file and then save it. The feed file is a TAB delimited one. For example: grep 539439AE9 file1 100.00000 20090119 20090119 20090521 ... (4 Replies)
Discussion started by: filter
4 Replies

6. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

7. Shell Programming and Scripting

Delete all lines after a specific line ?

Hello. My file is like this: a b c d e f g h i I want to delete all lines after the 3rd line, means after the "c". Is there any way to do this? The lines differ between them and the lines I want to delete does not have a specific word, or the lines I want to keep (a,b,c) does not have a... (4 Replies)
Discussion started by: hakermania
4 Replies

8. Shell Programming and Scripting

Find specific line and delete line after.

I'm looking for a way to search a file, in this case init.rc for a specific match, service adbd /sbin/adbd, and delete the a specific line after it Original: # adbd is controlled by the persist.service.adb.enable system property service adbd /sbin/adbd disabled After deletion: #... (5 Replies)
Discussion started by: GabrialDestruir
5 Replies

9. Shell Programming and Scripting

Delete specific line containing null after '='

Hi, I'm genrating a file from sql and the file looks like : $value1=A $value2=B $value3= $value4= $value5=C I want to delete those two lines which has Null after '='. Could you please guide me how to do it either using sed or awk ? (9 Replies)
Discussion started by: santanu83_cse
9 Replies

10. Shell Programming and Scripting

How would i delete a line at specific line number

Hi guys , I m writing a script to delete a line at particular location. But i m unable to use variable for specifying line number. for example. sed -n '7!p' filename works fine and deletes 7th line from my file but sed -n '$variable!p' filename gives following error. sed: -e... (12 Replies)
Discussion started by: pinga123
12 Replies
Login or Register to Ask a Question