LinuxQuestions.org
Help answer threads with 0 replies.
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 07-11-2017, 03:43 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
[Closed] Using awk to get 2 strings from a file


Lets say that i have this file foo.txt

Quote:
John 0 56 admin
Marc 0 3 user
Peter 1 52 user
Alexander 3 65 user
...
I want awk to get variables : 1 and 4 from each line in that file .

And i want a final output file like this :

Quote:
John Role: admin
Marc Role: user
Peter Role: user
Alexander Role: user
I want to grab with awk the $1 and 4$ and write a new file output like it is before .

How can this be done ?

Last edited by pedropt; 07-12-2017 at 02:52 AM.
 
Old 07-11-2017, 04:04 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
John 0 56 admin
Marc 0 3 user
Peter 1 52 user
Alexander 3 65 user
... this awk ...
Code:
awk '{print $1,"Role:",$4}' $InFile >$OutFile
... produced this OutFile ...
Code:
John Role: admin
Marc Role: user
Peter Role: user
Alexander Role: user
Daniel B. Martin
 
1 members found this post helpful.
Old 07-11-2017, 04:08 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
You have given the answer in your question, all you need to do is give it a try! By the time you posted this question you could have had it working!

Substitute "print" for "write" and and format the print output using those fields.

**danielbmartin beat us both to it!

Last edited by astrogeek; 07-11-2017 at 04:10 PM.
 
Old 07-11-2017, 04:37 PM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Thanks i did not knew this was possible to do with awk
Quote:
print $1,"Role:",$4
I am on windows know , i will change to linux and test it and then i will close this threat if that is the case .
 
Old 07-11-2017, 07:55 PM   #5
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Just for fun, here's a Perl version:
Code:
| => perl -e 'for (<>) {printf "%s Role: %s\n", (split/\s+/)[0,3]}' < role.txt
John Role: admin
Marc Role: user
Peter Role: user
Alexander Role: user
| =>

Last edited by Laserbeak; 07-11-2017 at 07:58 PM.
 
Old 07-12-2017, 02:51 AM   #6
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
It is working perfectly Daniel , sorry for the delay .
Also thank you laserbeak for the tip .
 
Old 07-12-2017, 06:13 AM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Just for fun, here's a sed version ...
Code:
sed -r 's/([^ ]+)(.* )([^ ]+)/\1\ Role: \3/' $InFile >$OutFile
I consider this inferior to the awk solution in post #2 because it is less readable.

Daniel B. Martin
 
Old 07-12-2017, 07:29 AM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
just for fun.

There'a windoze version called gawk.

ok
 
Old 07-13-2017, 01:12 PM   #9
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by AnanthaP View Post
just for fun.

There'a windoze version called gawk.

ok
gawk is really for everything. The Linux awk is really gawk.
 
  


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] more simple way to list all matched strings with awk luofeiyu Programming 12 04-14-2017 03:59 AM
search 2 strings using awk lokesh.talk Linux - Newbie 5 05-04-2015 08:18 AM
[SOLVED] AWK: match multiple strings in the file, print 1 when match and 0 when not cristalp Programming 12 11-15-2011 10:18 AM
BASH: replace strings in on file by the strings in another one cristalp Programming 5 10-28-2011 09:47 AM
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 02:40 PM

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

All times are GMT -5. The time now is 08:39 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