Grep/print/ a test file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep/print/ a test file
# 8  
Old 05-19-2015
vgersh99's proposal does work on my (Free)BSD system, but not quite on my linux mawk, as the line feeds are not used as field separators there. Try using FS="[:\n]" instead.
# 9  
Old 05-19-2015
Thanks Mr. Don. Cause I really don't understand how what vgersh99 suggested. Could you help me correct this code?

Code:
#!/bin/ksh
awk abc.txt | BEGIN {
RS=""
FS=":"
OFS="|"
split("Filename|Records|Unzipped Bytes ", t, "|")
for(i=1; i in t;i++)
namesA[t[i]]=i
}
{
for(i=1; i<=NF;i=i+2)
if ($i in namesA)
printf("%s", (namesA[$i]==1)?$(i+1):OFS $(i+1))
print ""
} >> newfile.txt

# 10  
Old 05-19-2015
Run it like
Code:
awk '
BEGIN   {RS=""
         FS="[:\n]"
         OFS="|"
         for (i=split("Filename|Records|Unzipped Bytes ", t, "|"); i; i--) namesA[t[i]]=i
        }
        {for(i=1; i<=NF;i=i+2)
         if ($i in namesA)
         printf("%s", (namesA[$i]==1)?$(i+1):OFS $(i+1))
         print ""
        }
' abc.txt

# 11  
Old 05-19-2015
And to append the output from the awk script to newfile.txt, change the last line RudiC suggested to:
Code:
' abc.txt >> newfile.txt

Note that I said append not replace; if you want to replace the contents of newfile.txt, use:
Code:
' abc.txt > newfile.txt

Either of the above will create newfile.txt if it did not already exist.
# 12  
Old 05-19-2015
Thanks Mr RudiC & Don. I still get syntax somehow
/staging/dotran :cat abctest.ksh

Code:
#!/bin/ksh
awk '
BEGIN   {RS=""
         FS="[:\n]"
         OFS="|"
         for (i=split("Filename|Records|Unzipped Bytes ", t, "|"); i; i--) namesA[t[i]]=i
        }
        {for(i=1; i<=NF;i=i+2)
         if ($i in namesA)
         printf("%s", (namesA[$i]==1)?$(i+1):OFS $(i+1))
         print ""
        }
' abc.txt

/staging/dotran :./abctest.ksh
awk: syntax error near line 8
awk: illegal statement near line 8
awk: syntax error near line 9
awk: illegal statement near line 9
# 13  
Old 05-19-2015
Hey Don, where's your usual line?

If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk . (Don Cragun)
This User Gave Thanks to RudiC For This Post:
# 14  
Old 05-19-2015
uname -a
Code:
SunOS scorpion 5.10 Generic_147147-26 sun4v sparc sun4v

I changed to nawk and get different error.

/staging/dotran :cat abctest.ksh
Code:
#!/bin/ksh
nawk '
BEGIN   {RS=""
         FS="[:\n]"
         OFS="|"
         for (i=split("Filename|Records|Unzipped Bytes ", t, "|"); i; i--) namesA[t[i]]=i
        }
        {for(i=1; i<=NF;i=i+2)
         if ($i in namesA)
         printf("%s", (namesA[$i]==1)?$(i+1):OFS $(i+1))
         print ""
        }
' abc.txt

/staging/dotran :./abctest.ksh
nawk: newline in character class [:
]...
input record number 1, file abc.txt
source line number 7
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep file and print value only if the following line doesnt contain value xxxx

Hi All, I have some large files which I would like to obtain some data from, I want to grep (or alturnative method) out certain values hut only if the folowing line doesnt contain a certain value. I will explain better below showing an example print of the data. NE : V0001 NE : V0002... (6 Replies)
Discussion started by: mutley2202
6 Replies

2. Shell Programming and Scripting

Grep in file and print in the line

hi # cat test.txt Test Date: 20131008 1515 -------------------------------------------------------------------------------------------------------------- Saxx = Proc_m0_s13 : 1640 Saxx = Proc_m0_s15 : 1791 Saxx = Proc_m0_s17 ... (2 Replies)
Discussion started by: justbow
2 Replies

3. Shell Programming and Scripting

Grep or print each section of a file on one line with a separator

I can obtain information from itdt inventory command however it display as below, I'd like to print each entity on one line but seperated by : the file is something like and each section ends with Volume Tag Drive Address 256 Drive State ................... Normal ASC/ASCQ... (3 Replies)
Discussion started by: gefa
3 Replies

4. Shell Programming and Scripting

Script to process file from a directory and grep the required content and print

Hi All, below script reads the perticular files from the directory. Am trying to fetch status and print them in the required format. It needs to read line and search for string "Passed/Failed" and print them under correct sub header. script : BASE_DIR=/tmp/test/REPORT/CollectReport #... (16 Replies)
Discussion started by: Optimus81
16 Replies

5. UNIX for Dummies Questions & Answers

Print file name when running grep from within find

Solaris 10 When running grep from within find command (don't know the technical term for 'running from within' ) , find command returns only the line which contains the pattern. Is there any way to get the file name printed as well ? $ pwd /opt/testdir/anotherDir $ $ $ cat findme.txt... (3 Replies)
Discussion started by: omega3
3 Replies

6. Shell Programming and Scripting

grep/awk to only print lines with two columns in a file

Hey, Need some help for command to print only lines with two columns in a file abc 111 cde 222 fgh ijk 2 klm 12 23 nop want the ouput to be abc 111 cde 222 ijk 2 Thanks a lot in advance!!! (3 Replies)
Discussion started by: leo.maveriick
3 Replies

7. Shell Programming and Scripting

find file and print only contents with a hit by grep

Hi, can someone help me. I have some files and search a content in this files. If i have a hit I will print a output: filename:content But are more hits in one file: The output is always filename:content E.G. Seach about "three" file1 {one, two, three, four, three} file2... (5 Replies)
Discussion started by: Timmää
5 Replies

8. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies

9. UNIX for Dummies Questions & Answers

find file grep it and print file name

i am trying to search a few hundred release note text files for a certain word. however when i use the below command i can find a file that contains it but i dont know the file name. how can i change this command to output the name of the file that grep was successful in? find builds -name... (4 Replies)
Discussion started by: borderblaster
4 Replies
Login or Register to Ask a Question