LinuxQuestions.org
Review your favorite Linux distribution.
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 08-11-2017, 12:05 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 81

Rep: Reputation: Disabled
[SOLVED] Grab multiples lines strings and write to new file .


Here is what i am trying to achieve .

Example File A

Quote:
john dfsfsd ewwerwer 23244 admin
john 424324 hfhfjd saturday
marc egrerg wdfweggew 87556 user
will ddoers ehaslept 91726 user
will ergrg rpgerg 3424234 monday
johnb erejgr owffewf 463346 user
smith gerereg gtettgetg 28293 user
smith rfaofrj errpgerg 22930 thursday
.....
I want to grab some exact variables from this file and write a new file with those vars on single line .

Objective :

get from file A to file B :
john admin saturday
will user monday
smith user thursday


How can i do this ?

Last edited by pedropt; 08-11-2017 at 05:25 PM.
 
Old 08-11-2017, 12:11 PM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,557

Rep: Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810
I don't know how can you do this, I would try it using awk.
What have you tried so far?
 
Old 08-11-2017, 12:32 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,936
Blog Entries: 14

Rep: Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160Reputation: 1160
Code:
awk 'NR%2{printf "%s ",$0;next;}1' FILEA |awk '{print $1, $5,$NF}' >FILEB
Note that the line that has "johnb" as is "marc" on it is unmatched in your sample (i.e. johnb appears only once) so it skews the ouput.

john admin saturday
marc user user
will monday user
smith user thursday

Last edited by MensaWater; 08-11-2017 at 12:35 PM.
 
Old 08-11-2017, 12:35 PM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 81

Original Poster
Rep: Reputation: Disabled
The only way to do this is to check on file if the first words from each line are equal and one after another .
If that is the case then user awk to read both lines and pick up the variables .

I can only do it by searching for a common string like :

Code:
grep "john" filea | awk '{print$1,$5}'
Output from
Code:
awk 'NR%2{printf "%s ",$0;next;}1' filea |awk '{print $1, $5,$NF}'
Quote:
john admin saturday
marc user user
will monday user
smith user thursday
John & Smith are ok , but the others are not

Last edited by pedropt; 08-11-2017 at 12:39 PM.
 
Old 08-11-2017, 12:51 PM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,557

Rep: Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810Reputation: 2810
this is a two-phase-process, first you collect the info, and finally you can print. You need to use an associative array, like this:
Code:
awk ' { vars[$1] += $NF }
      END { print vars array }'
(this won't work, you need to complete it)
 
Old 08-11-2017, 01:09 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,572

Rep: Reputation: 478Reputation: 478Reputation: 478Reputation: 478Reputation: 478
Quote:
Originally Posted by pedropt View Post
Objective :

get from file A to file B :
john admin saturday
will user monday
smith user thursday
Is it always true that the matching data (john, will, and smith) will appear in adjacent lines? That's how it is in your sample input file but can we count on it to always be so?

Daniel B. Martin
 
Old 08-11-2017, 02:33 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,572

Rep: Reputation: 478Reputation: 478Reputation: 478Reputation: 478Reputation: 478
With this InFile ...
Code:
john dfsfsd ewwerwer 23244 admin
john 424324 hfhfjd saturday
marc egrerg wdfweggew 87556 user
will ddoers ehaslept 91726 user
will ergrg rpgerg 3424234 monday
johnb erejgr owffewf 463346 user
smith gerereg gtettgetg 28293 user
smith rfaofrj errpgerg 22930 thursday
... this awk ...
Code:
awk '{if (!a[$1]) a[$1]=$1" "$5; 
             else a[$1]=a[$1]" "$NF}
      END {for (j in a)
  if (length(a[j])>15) print a[j]}'  \
$InFile >$OutFile
... produced this OutFile ...
Code:
john admin saturday
smith user thursday
will user monday
Daniel B. Martin
 
1 members found this post helpful.
Old 08-11-2017, 05:24 PM   #8
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 81

Original Poster
Rep: Reputation: Disabled
Nice work Daniel , it is working perfectly .
Thank you
 
Old 08-11-2017, 08:50 PM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,795

Rep: Reputation: 2157Reputation: 2157Reputation: 2157Reputation: 2157Reputation: 2157Reputation: 2157Reputation: 2157Reputation: 2157Reputation: 2157Reputation: 2157Reputation: 2157
nm - musta been asleep.
 
Old 08-12-2017, 10:46 AM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,572

Rep: Reputation: 478Reputation: 478Reputation: 478Reputation: 478Reputation: 478
The solution offered in post #7 stumbles on duplicate names.
Here is an improved version which handles dupes and is shorter.

With this InFile ...
Code:
john dfsfsd ewwerwer 23244 admin
john 424324 hfhfjd saturday
marc egrerg wdfweggew 87556 user
will ddoers ehaslept 91726 user
will ergrg rpgerg 3424234 monday
johnb erejgr owffewf 463346 user
smith gerereg gtettgetg 28293 user
smith rfaofrj errpgerg 22930 thursday
will ddoers gargle 92471 user
will ergrg flipper 3141593 friday
... this awk ...
Code:
awk '{if (!a[$1]) a[$1]=$1" "$5; 
  else {print a[$1]" "$NF; a[$1]=""}}'  \
$InFile >$OutFile
... produced this OutFile ...
Code:
john admin saturday
will user monday
smith user thursday
will user friday
Daniel B. Martin
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] search for exact strings from one file to another and write if not exist pedropt Programming 13 07-19-2017 01:30 PM
How to remove lines and parts of lines from python strings? golmschenk Programming 3 11-26-2009 11:29 PM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM
Using c++ to write to a text file. Im having problem with strings swatward Programming 4 02-03-2005 09:17 PM

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

All times are GMT -5. The time now is 09:58 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration