LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2014, 02:10 AM   #1
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Rep: Reputation: Disabled
Question while read and output from two files


hello
I am looking to do this with same command approach I have two files i just want to gerp the last vaule in the line which is the number if is there or show unknown if does not

file1
Code:
114444444,2,FTTH,DTN,No Dial tone & No Browsing,FBR,Fiber Major Fault,11-444-4444
112221111,2,FTTH,CCO,Can't call some lines,ERR,Err Opening TT#,11-222-1111

using
Code:
cat file1 | awk -F, 'OFS=","{print $4,$5,$6,$7"+"$8}' |  while IFS=+ read f1 f2;do echo -n $f1",";cat file2 | grep $f2 ;done
output
Code:
DTN,No Dial tone & No Browsing,FBR,Fiber Major Fault,CCO,Can't call some lines,ERR,Err Opening TT#,"116-00_MRSTR8888L9:1-1-11-111     116-00_MRSTRD1111    11-222-1111
I am looking to have this output

Code:
DTN,No Dial tone & No Browsing,FBR,Fiber Major Fault,unknown
CCO,Can't call some lines,ERR,Err Opening TT#,"116-00_MRSTRD00OL9:1-1-12-4-12     116-00_MRSTRD1111    11-222-1111

Last edited by phpshell; 05-21-2014 at 02:13 AM.
 
Old 05-21-2014, 02:21 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Hi,
file2 is still unknown, and I do not understand what do you really need.
But I have a few comments:
Code:
instead of:
cat file | awk ' ...'
use
awk '...' file
....
also, instead of
cat file | grep  ....
use
grep .... file
I think all these things can be solved with a single awk script, but first we need to know exactly what do we need to implement....
 
Old 05-21-2014, 02:28 AM   #3
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
thanks Pan64

sorry file2

Code:
"116-00_MRSTRD00OL9:1-1-12-4-12    116-00_MRSTRD1111    11-222-1111
"220-00_MRSTRD00OL9:1-1-12-4-124     116-00_MRSTRD1111    11-333-5554
"333-00_MRSTRD00OL9:1-1-12-4-121     116-00_MRSTRD1111    11-555-5111
"444-00_MRSTRD00OL9:1-1-12-4-124     116-00_MRSTRD1111    11-222-5555
just I want to grep 11-222-1111 from file1 also some field should be appear from file 1

Code:
DTN,No Dial tone & No Browsing,FBR,Fiber Major Fault,unknown
CCO,Can't call some lines,ERR,Err Opening TT#,"116-00_MRSTRD00OL9:1-1-12-4-12     116-00_MRSTRD1111    11-222-1111
 
Old 05-21-2014, 06:02 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So maybe something like:
Code:
awk 'BEGINFILE{if(x)FS=",";x=!(FNR);OFS=","}NR==FNR{a[$NF]=$0;next}{if(!($NF in a))u="unknown";print $4,$5,$6,$7,(u?u:a[$NF]);u=""}' file2 file1
 
1 members found this post helpful.
Old 05-22-2014, 12:30 AM   #5
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
thanks a lot grail
is there any way to do it by while? .because it is very difficult for me to trace it on awk
 
Old 05-22-2014, 05:49 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
awk has done all the heavy lifting so you would need to explain what you mean by trace? What to you is the use of the bash while loop? Especially after pan64 has explained that your current pasting together of multiple commands is a poor way to proceed.
 
Old 05-22-2014, 05:55 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
awk has a built-in while loop. I would suggest you to go thru that script and try to understand it. Use the man page of awk and ask if something is not clear...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk question - read in txt files, offset data by given amount, output new txt files pomico Programming 19 09-17-2012 11:43 AM
read three files and print output in a new file kvtspavan Linux - Newbie 17 09-10-2010 06:43 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
[SOLVED] Read large text files (~10GB), parse for columns, output vache Programming 57 04-07-2009 10:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration