Read a file with multiple tags


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read a file with multiple tags
# 1  
Old 01-30-2015
Read a file with multiple tags

Hi,

I have to read a file which has values. Now this file has multiple values for tag "total requests". I have been asked to read it and then rewrite the multiple values in a different csv file. I have been trying but I only get a single value in the csv. Please find below pasted shell script and help me out please.

Thanks a lot for all the help extended


Code:
#!/bin/bash

for a in `cat $filename | grep "Total Request\s*:"|sed 's/   *//g'| wc -l`;

 do  cat $filename |grep "Total Request\s*:"|sed 's/   *//g'|awk -F":" 'END {print $1,$2}' >>NewFile.csv; 

done;

# 2  
Old 01-30-2015
Can you post sample input and desired output
# 3  
Old 01-30-2015
You get a single value as you print just one record in the END section. Print it for every line...
And, one awk command will do instead of that pipe organ that you present.
And, as anbu23 says, samples will always help us to help you.
# 4  
Old 01-30-2015
Hi,

Please find below sample input

====================================
Code:
=========For Time =======Tue Jan 27 16:25:11 IST 2015======

    GTP' Service Request Summary
------------------------------------
 Total Request                                :91
 Total Echo Request                           :6


=========For Time =======Tue Jan 27 16:27:11 IST 2015======

    GTP' Service Request Summary
------------------------------------
 Total Request                                :100
 Total Echo Request                           :7

the file is in .txt format and contains around 20-30 more entries of the same format as pasted above

the output is required in .csv format and contains the above Inputs & their values i.e Total Request in 1 coloumn & Total Echo request in the second coloumn

Thanks a lot for all the help again.

Moderator's Comments:
Mod Comment edit by bakunin: please see below

Last edited by bakunin; 01-30-2015 at 07:52 AM.. Reason: wanted to complete the statement as to not create an issue.
# 5  
Old 01-30-2015
Please use code tags as required by forum rules!

---------- Post updated at 11:42 ---------- Previous update was at 11:35 ----------

Your specification still is extremely sparse. Try this
Code:
awk '/^Total.*Request/ {printf "%s%s", $0, (++c%2)?";":"\n"}' file
Total Request :91;Total Echo Request :6
Total Request :100;Total Echo Request :7

and DON'T come back telling us "It doesn't work" or "I don't like the format"!
This User Gave Thanks to RudiC For This Post:
# 6  
Old 01-30-2015
Dear Rudi ,

Firstly, the entries mentioned in my previous reply are just entires in a .txt file, hence, i did not use the code tag

Secondly, I am extremely grateful for all the help extended. You have been really nice in helping me out.

Just for understanding. I am pasting the entire script for reference. Please do correct me if I am wrong. I am not that good at scripting

Code:
#!/bin/bash

for a in `cat $filename | grep "Total Request\s*:"|sed 's/   *//g'| wc -l`;

 do  cat $filename |grep "Total Request\s*:"|sed 's/   *//g'|awk '/^Total.*Request/ {printf "%s%s", $0, (++c%2)?";":"\n"}'>>NewFile.csv; 

done;

Thanks a lot again
# 7  
Old 01-30-2015
Sorry, I don't understand your code. You are running a for loop without using its loop variable a anywhere. To what avail?
Try running just
Code:
awk '/^Total.*Request/ {printf "%s%s", $0, (++c%2)?";":"\n"}' $filename

and redirect its output to "Newfile.csv" if happy. No cat, no grep, no sed...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to read file and run multiple jobs

I have a txt file line1 line2 line3 $!/bin/sh cat /tmp/lus.txt | while read line do esxcli storage vmfs unmap -u $lin -n 4000 done this works but does in one line at a time. how do I do all lines at once simutaeously? Please use CODE tags as required by forum rules! (4 Replies)
Discussion started by: tdubb123
4 Replies

2. Shell Programming and Scripting

Read multiple lines at a time from file

Hello All, I have a file like below.... dn: cn=user1,ou=org,o=org cn=user1 uid=user1 cn=user2,ou=org,o=org cn=user2 uid=user2 cn=user3,ou=org,o=org cn=user3 cn=user33 uid=user3 cn=user4,ou=org,o=org cn=user4 uid=user4 (6 Replies)
Discussion started by: s_linux
6 Replies

3. Shell Programming and Scripting

Read multiple text file in shell sccript

I need help. I need to read all current and incoming text file in the directory and change it's content to 0. The following code change the content of the text file to 0, however, the code always assumes that the files are ALREADY present. Is there any way where I can change the file1_.txt,... (3 Replies)
Discussion started by: jasperux
3 Replies

4. Shell Programming and Scripting

Read from Multiple character delimited flat file

Hello Guys I have a multiple character delimited flat file "|~|". when I tried to read the data the "|" character also coming Example I/P file 9882590|~|20111207|~|K03501000063005574033961|~|K|~| Command to get the second column I used awk -F"|~|" ' {print $2}' ... (2 Replies)
Discussion started by: Pratik4891
2 Replies

5. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (1 Reply)
Discussion started by: erlanq
1 Replies

6. Shell Programming and Scripting

How to read a multiple lines from a file n executing them?

Hi all, I am just trying to read the contents of a file. basically this file has a list of dat files. then i want to access these dat files n execute a script on them one by one using a loop. i hav e written like this ls -l | cut -c 58-88 > file1.txt while do arr1="$( sed -n '1p'... (7 Replies)
Discussion started by: navjyotisonu5
7 Replies

7. Shell Programming and Scripting

Read 1-line file and separate into multiple variables

I have one line files with 17 records separated by a semi-colon. I need to create a variable from each record, which I can do via a separate awk for each one, but I know there has to be a better way. Along with pulling out the variable, I need to convert some url coding like a + to a space, etc.... (4 Replies)
Discussion started by: numele
4 Replies

8. Shell Programming and Scripting

Read a file with in UNIX and send multiple mails

Hi-I want to create a shell script which should read a file line by line (file having email address and transaction id of each user)and send email on email ids with the transaction id of the user respectively. Please help - I think a while loop should help but I am very new too UNIX Shell... (1 Reply)
Discussion started by: DeepSalwan
1 Replies

9. Shell Programming and Scripting

How to read multiple line from log file

I have errors in the log that span multiple lines and I can find say the 2nd line in the log of this error using an unique word. However, this only gets me the line that the word appears in not the full error which may be 3 or four line long. So if there way to display say the line before a match... (4 Replies)
Discussion started by: vishal_vsh1
4 Replies

10. Shell Programming and Scripting

How to delete multiple space or tabs from a read only file

Hi, Actually I am want to cut the three fields of "file-nr" file. $ cat /proc/sys/fs/file-nr 638 219 52270 I want to assign these value to diffrent varibales as follow:- a=638 b=219 c=52270 I tried to use cut command for this purpose and also tried to squeeze all sapces... (6 Replies)
Discussion started by: bisla.yogender
6 Replies
Login or Register to Ask a Question