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 09-01-2011, 04:40 PM   #1
itburnz
LQ Newbie
 
Registered: Sep 2011
Distribution: Debian & Arch
Posts: 2

Rep: Reputation: Disabled
Reformatting textfiles in bash


Dear all,

I am processing a large set of text files that I want to split in half by columns. What I have works, but I'm keen to learn a cleaner better way of approaching this issue.

The text files look somewhat like the following:

Code:
ab cd ef gh
ij kl mn op
I want to take the first two elements in each line and dump them into a new file. What I have come up with is this:

Code:
for i in {1..2} ; do head -n $i text | tail -n 1 | awk -F" " '{ for (i=1; i<=2; i++) { printf "%s ", $i } }' | sed -e 's/\s$/\n/g'; done
Essentially I loop over the file taking one line at a time with head and tail, pass the line to awk where I grab the first two elements, and lastly I replace the last space in the line with a newline character.

The output looks like this:

Code:
ab cd
ij kl
It does what it is supposed to, but I have the feeling that there must be a better solution to my problem.

Thanks!
 
Old 09-01-2011, 04:57 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Code:
cut -c 1-5 input.txt
 
Old 09-01-2011, 07:10 PM   #3
itburnz
LQ Newbie
 
Registered: Sep 2011
Distribution: Debian & Arch
Posts: 2

Original Poster
Rep: Reputation: Disabled
Smile

Thanks! I didn't realize I could do this that easy with cut.
 
  


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
Upload zip with textfiles via http --> import into MYSQL Latios Programming 1 06-14-2011 01:18 PM
[SOLVED] Reformatting file to be a table in a bash script emmalg Linux - Newbie 14 11-24-2010 08:15 PM
reformatting hd alex_ar Linux - Software 1 08-30-2005 12:55 PM
A little reformatting to do. Ree Linux - Newbie 16 10-04-2004 02:26 PM
edit textfiles in aterm Longinus Linux - Newbie 3 02-18-2004 09:21 PM

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

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