Awk print to file problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk print to file problem
# 1  
Old 03-03-2011
Awk print to file problem

Hello,

I have a file seperated with "|" I want to search the 11th field if it matches certain words change it to an empty space.

I have managed to do that, but now I need it to replace the file.

this is my code:

Code:
 
awk 'BEGIN{OFS=FS="|"}$11=="to follow"||$11=="tbc"||$11=="123456"{$11=" "}{print}' file

i tried doing a print > file, print >"file" and a print $* > "file" but I still get the output on the screen and the file is not updated. Any ideas?

Thanks in advance
Tasos
# 2  
Old 03-03-2011
Hope this helps.

Code:
awk 'BEGIN{OFS=FS="|"}$11=="to follow"||$11=="tbc"||$11=="123456"{$11=" "}{print}' < file > newfile


Last edited by Franklin52; 03-03-2011 at 10:15 AM.. Reason: Please use code tags, thank you
This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 03-03-2011
sort it, print > "file" worked, for some reason it did not before, i just deleted the script and wrote it again and it worked.

thanks anyway

---------- Post updated at 04:20 PM ---------- Previous update was at 02:17 PM ----------

ok now I have a new problem, I am sure I saw a similar post here but wasn't able to locate it.

What I am trying to do now is have the script looking into a folder, if there are any files matching a prefix, then execute the awk command and update them, here is the code:

Code:
 
CMR='mypath'
ls $CMR |grep TEST_
var1=$(ls $CMR | grep TEST_)
if [ $? -eq 0 ]
then
  for filename in $var1
  do
 
    awk 'BEGIN{OFS=FS="|"}$11=="to follow"||$11=="tbc"||$11=="123456"{$11=" "}{print > "$filename"}' $filename

done
fi

however this does not update the files.
Any ideas what the syntax might be to print > an iteretive variable?

thanks in advance
tasos
# 4  
Old 03-03-2011
You really can't edit-in-place like that. awk doesn't work that way. It'll either do nothing, or trash the entire file.

Just print in awk instead of printing to file, and do the redirection outside awk.

That's also a useless use of ls | grep, you can just glob it like this:

Code:
for FILE in "${CMR}"/TEST_*
do
        # Store output in temporary file /tmp/$$
        # then overwrite the original's contents
        awk '{stuff}' < "$FILE" > /tmp/$$ &&
                cat /tmp/$$ > "$FILE"
done

rm -f /tmp/$$

Only allow cat to overwrite it once you're very very sure awk is doing what you want.

Last edited by Corona688; 03-03-2011 at 12:30 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 03-03-2011
Sadly the above did not work, the awk command was not executed at all or it replaces the amended file with the original again
# 6  
Old 03-03-2011
hope this can help you.
Code:
sed -i -r -e 's/\|/\n/10' -e 's/\n(to follow||tbc||123456)/| /' file

# 7  
Old 03-03-2011
Quote:
Originally Posted by Corona688
That's also a useless use of ls | grep, you can just glob it like this:
The ls | grep wasn't entirly useless it was testing for existance of a file matching the glob to avoid issues if no TEST_* files are in the dir.

TasosARISFC, the {stuff} was supposed to be your actual awk script not to be inserted literally

This should be close to what you need (checks for no TEST_ files; {stuff} now replaced with awk code to do the job):
Code:
if [ ! "$CRM"/TEST_* = "$CRM"'/TEST_*' ]
then
    for FILE in "${CMR}"/TEST_*
    do
        # Store output in temporary file /tmp/$$
        # then overwrite the original's contents
        awk -F\| '$11~/(tbc|123456|to follow)/{$11=x} 1' OFS="|" < "$FILE" > /tmp/$$ &&
            cat /tmp/$$ > "$FILE"
    done
    rm /tmp/$$
fi


Last edited by Chubler_XL; 03-03-2011 at 08:04 PM..
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

awk print even fields of file

Hello: I want to print out the even number of fields plus the first column as row identifiers. input.txt ID X1 ID X2 ID X3 ID X4 A 700 A 1200 A 400 A 1300 B 2000 B 1000 B 2000 B 600 C 1400 C 200 C 1000 C 1200 D 1300 D 500 D 600 D 200and the output is: output.txt ID X1 X2 X3... (3 Replies)
Discussion started by: yifangt
3 Replies

3. Shell Programming and Scripting

awk print to file

I have a big c file that gives a lot of output that I don't care about right now. I wrote this awk script to do mostly what I want. Not sure how to put the finishing touches on it. I would also like it to write to a file when it gets the appropriate condition as true. Is this possible with awk? Or... (6 Replies)
Discussion started by: cokedude
6 Replies

4. Shell Programming and Scripting

Not getting array in .awk file and print it

I have test.sh file as below : set -A IDARR $ID echo | awk -f test.awk -v TempArr="${IDARR }" I have test.awk file as below : BEGIN { Flag = 1; } { print "Hello"; for(i in TempArr) { print i; } } (9 Replies)
Discussion started by: nes
9 Replies

5. Shell Programming and Scripting

awk print output problem

Hello friends, I have written a script and i need to add some part into it so that i could print out more results depending on more conditions, This is the core part of the script which does the actual work: echo "$j" && nawk -v stat=$2 'NR==FNR &&... (1 Reply)
Discussion started by: EAGL€
1 Replies

6. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

7. Shell Programming and Scripting

problem with print append to output file syntax

I'm trying to output the contents of the infile to the outfile using Append. I will want to use append but the syntax doesn't seem to be working ! Input file (called a.txt) contains this: a a a b b b I'm running shell script (called k.sh) from Unix command-line like this: ./k.sh .... (1 Reply)
Discussion started by: script_op2a
1 Replies

8. Shell Programming and Scripting

Compare and print out data only appear in file 1 problem

Below is the data content of file_1 and file_2: file_1 >sample_1 FKGJGPOPOPOQA ASDADWEEWERE ASDAWEWQWRW ASDASDASDASDD file_2 >sample_1 DRTOWPFPOPOQA ASDADWEEASDF ASDADRTYWRW ASDASDASDASDD I got try the following perl script. Unfortunately, it can't give my desired output result... (7 Replies)
Discussion started by: patrick87
7 Replies

9. Homework & Coursework Questions

Problem with awk,not able print the file that is greater than 3000 bytes.

My Script: #!/bin/sh date=`date +%y%m%d -d"1 day ago"` in_dir=/vis/logfiles/to_solmis cp `grep -il ST~856~ $inbound_dir/*$date*` /vis/sumit/in_ASN/ for i in /vis/sumit/in_ASN/* do mkdir -p /vis/sumit/inboundasns.$date cp `echo $i`... (1 Reply)
Discussion started by: shrima.pratima
1 Replies

10. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies
Login or Register to Ask a Question