LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-08-2012, 02:45 PM   #1
Leisurely Gentlemen
LQ Newbie
 
Registered: May 2012
Posts: 3

Rep: Reputation: Disabled
How to grep sections of a line out and copy to new file


I have to grep out only parts of the lines and put into a new file

Lines look like this ie exampleA,exampleB,exampleC,exampleD

Now I only want exampleA,exampleB in my new file. How can I only select the first 2?

I hope this was clear enough. Thanks!

LG
 
Old 05-08-2012, 02:57 PM   #2
Nermal
Member
 
Registered: Jan 2009
Distribution: Debian
Posts: 59
Blog Entries: 2

Rep: Reputation: 6
Code:
awk -F',' '{print $1","$2}' file_name
If you need to search then see this thread: http://www.linuxquestions.org/questi...ecords-942033/
 
Old 05-08-2012, 03:39 PM   #3
Leisurely Gentlemen
LQ Newbie
 
Registered: May 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you.. Can you show me how to move it into a new file after this awk -F',' '{print $1","$2}' file_name
 
Old 05-08-2012, 03:43 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Read the section in the bash guide on input/output for how to do file redirections.

http://mywiki.wooledge.org/BashGuide

Then go back to the beginning and read the whole thing.

And actually, cut would be more efficient for a simple job like this.

Code:
cut -d ',' -f 1-2 file > newfile
 
Old 05-08-2012, 03:44 PM   #5
Leisurely Gentlemen
LQ Newbie
 
Registered: May 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
Read the section in the bash guide on input/output for how to do file redirections.

http://mywiki.wooledge.org/BashGuide

Then go back to the beginning and read the whole thing.

And actually, cut would be more efficient for a simple job like this.

Code:
cut -d ',' -f 1-2 file > newfile
Thank you sir!
 
  


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
C - Copy File Line By Line With Some Slight Changes golmschenk Programming 11 03-03-2010 01:46 PM
How to grep two strings in one line in a file? thomas2004ch Linux - Newbie 5 09-15-2009 04:49 AM
How to grep more than one line from a file markotitel Linux - Software 10 09-03-2009 06:33 AM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 10:51 AM
grep a line in a file from dir bkeating Linux - Newbie 4 07-24-2003 02:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02: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
Open Source Consulting | Domain Registration