Awk print to file problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk print to file problem
# 8  
Old 03-04-2011
Thank you for you reply, I was using the original code inside {stuff} , I did not take it literrally :P

The code above works, but not as I wanted, my code previously was explicity maching the search criteria, now it will replace the 11th field with a space if it finds a word matching, for example:

field 11= tbc ----gets replaced by " "
field 11= 0005533 tbc --- also gets replaced by " "

I want it to replace only the first case, e.i. explicitly matching the field.

another example

field 11= number to follow --- should not be replaced
field 11= to follow --- this should be replaced

---------- Post updated at 03:52 PM ---------- Previous update was at 03:08 PM ----------

This is what eventually worked for me. It does exactly what I want.
Chubler_XL in you reply the if check I think its wrong.

Anyway this is the code that worked:

Code:
 
CRM='mypath'
ls $CRM | grep TEST
if [ $? -eq 0 ]
  then
    for FILE in "${CRM}"/TEST*
    do
        # Store output in temporary file /tmp/$$
        # then overwrite the original's contents
        awk -F\| '$11=="to follow"||$11=="test"||$11=="tbc"||$11=="123456"||$11=="TBC"{$11=""} 1' OFS="|" < "$FILE" > /tmp/$$ &&
            cat /tmp/$$ > "$FILE"
 
    done
  rm /tmp/$$
fi

Thank you all for your help
# 9  
Old 03-04-2011
Code:
if [ ! "$CRM"/TEST_* = "$CRM"'/TEST_*' ]

That will fail with "too many arguments" whenever there's more than one directory and bomb the whole script!

I usually try something like
Code:
for FILE in "${CMR}"/TEST_*
do
    [ -d "$FILE" ] || continue # Don't run awk on nonexistent dirs
    # 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 -f /tmp/$$

...but even without the 'continue' check nothing bad will happen beyond awk printing an error message.

---------- Post updated at 12:03 PM ---------- Previous update was at 12:01 PM ----------

Quote:
Originally Posted by TasosARISFC
Code:
 
CRM='mypath'
ls $CRM | grep TEST
if [ $? -eq 0 ]
  then
    for FILE in "${CRM}"/TEST*
    do
        # Store output in temporary file /tmp/$$
        # then overwrite the original's contents
        awk -F\| '$11=="to follow"||$11=="test"||$11=="tbc"||$11=="123456"||$11=="TBC"{$11=""} 1' OFS="|" < "$FILE" > /tmp/$$ &&
            cat /tmp/$$ > "$FILE"
 
    done
  rm /tmp/$$
fi

Thank you all for your help
The "ls $CRM | grep TEST" does absolutely nothing now unless you just want it printing to console for some reason. You can remove it.
This User Gave Thanks to Corona688 For This Post:
# 10  
Old 03-06-2011
OK if you want exact match on that string list put ^ in front and $ on end.

So we have the following with Corona688's enhanced check (thanks given) for no TEST_ files:

Code:
for FILE in "${CMR}"/TEST_*
do
    [ -d "$FILE" ] || continue # Don't run awk on nonexistent dirs
    # 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 -f /tmp/$$

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