Grep/print/ a test file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep/print/ a test file
# 1  
Old 05-18-2015
Grep/print/ a test file

cat abc.txt
Code:
Filename: SHA_AED_Monthly_SNR_20150331.txt.gz
Data Format: ASCII with carriage returns and linefeeds
Compression: GZIP
GZIP Bytes: 36893068
Unzipped Bytes : 613794510
Records: 851310
Record Length: 738
Blocksize: 32472


Filename: SHA_AED_SNR_ChangeLog_20150331.txt.gz
Data Format: ASCII with carriage returns and linefeeds
Compression: GZIP
GZIP Bytes: 148288
Unzipped Bytes : 740507
Records: 3877
Record Length: 189
Blocksize: 32697


Filename: SHA_AED_SNR_OutletMaster_20150331.txt.gz
Data Format: ASCII with carriage returns and linefeeds
Compression: GZIP
GZIP Bytes: 8147188
Unzipped Bytes : 31502244
Records: 164837
Record Length: 199
Blocksize: 32636




I like get new output file (newfile.txt) to print (Filename|Records|Unzipped Bytes) only

Code:
 
 SHA_AED_Monthly_SNR_20150331.txt.gz|851310|36893068
 SHA_AED_SNR_ChangeLog_20150331.txt.gz|3877|740507
 SHA_AED_SNR_OutletMaster_20150331.txt.gz|164837|31502244

Could someone please help me with this script below why not work. Thanks

Code:
 
 #!/bin/ksh
  
 ls -1 abc.txt |while read FILE
 do
 Filename=`cat abc.txt |grep Filename |awk '{print $3}'`
 Record=`cat abc.txt |grep Records |awk '{print $3}'`
 Gunzip=`cat abc.txt |grep Unzipped |awk '{print $4}'`
 echo "$Filename|$Record|$Gunzip" >>  newfile.txt
 done


Last edited by vgersh99; 05-18-2015 at 06:44 PM.. Reason: code tags
# 2  
Old 05-18-2015
if your records are reliably uniform you could echo only if a test for a blank line.

WARNING UNTESTED LATE NIGHT CODE...
Code:
egrep '^$' && echo "$Filename|$Record|$Gunzip" >>  newfile.txt

# 3  
Old 05-18-2015
Thanks Skynesaver......so should be like this?
Code:
 
 #!/bin/ksh
ls -1 abc.txt |while read FILE
do
Filename=`cat abc.txt |grep Filename |awk '{print $3}'`
Record=`cat abc.txt |grep Records |awk '{print $3}'`
Gunzip=`cat abc.txt |grep Unzipped |awk '{print $4}'`
#echo "$Filename|$Record|$Gunzip" >>  newfile.txt
egrep '^$' && echo "$Filename|$Record|$Gunzip" >>  newfile.txt
done

# 4  
Old 05-18-2015
Actually, you should probably step through the file in a loop rather than your current approach...(Late night code warning remains in force but this is closer to working than the approach above Smilie )
Code:
#!/usr/bin/perl

open (my $data, '<', $ARGV[0]);
while (<$data>){
$record{$1}=$2 if (/(\S+)\s*:\s*(.+)$/);
}
if ((/^\s*$/) && ($record{Filesname} ne '')){
print join('|',@record{"Filename","Record","Unzipped Bytes"}),"\n";
$record{Filename}='';
}

# 5  
Old 05-18-2015
awk -f dot.awk abc.txt where dot.awk is:
Code:
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 ""
}


Last edited by vgersh99; 05-18-2015 at 07:11 PM.. Reason: removed the 'assumption' - works with the space before :
# 6  
Old 05-19-2015
Thanks vgersh99. Is this complete code? Somehow I can't make it work....please help. Thanks

Code:
 
 #!/bin/ksh
 cat 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

# 7  
Old 05-19-2015
Hi dotran,
Note that vgersh99 suggested using the awk utility (not the non-existent BEGIN utility).
And, awk is perfectly capable of reading files without creating a pipeline using cat to double the number of processes running and triple the number of bytes read and written to read your input file.

Please look more closely at what vgersh99 suggested and try what he suggested.
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