LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 03-27-2012, 03:24 AM   #1
arn2025
LQ Newbie
 
Registered: Feb 2012
Posts: 26
Blog Entries: 1

Rep: Reputation: Disabled
manipulating text


i want to edit this text file and remove the column with the arts and also edit the separator & so that they become columns.
old text

Code:
ANNE 2453 3564 M@M.U 1 O&C&TY
JUDE 3453 2345 n@m.u 1 W&Y&TY
wend 3442 3773 h@y.u 1 O&C&TY
fill 3266 7473 g@m.u 1 W&T&TY
feat 3455 5544 1 W&C&TY
carl 3435 6723 1 Y&C&TY
wade 3442 7843 1 L&C&TY
jenw 7863 9028 1 P&C&TY
matt 5637 9034 1 W&C&TY
to this

Code:
ANNE 2453 3564 1 O	C	TY
JUDE 3453 2345 1 W	Y	TY
wend 3442 3773 1 O	C	TY
fill 3266 7473 1 W	T	TY
feat 3455 5544 1 W	C	TY
carl 3435 6723 1 Y	C	TY
wade 3442 7843 1 L	C	TY
jenw 7863 9028 1 P	C	TY
matt 5637 9034 1 W	C	TY

Last edited by colucix; 03-27-2012 at 03:29 AM. Reason: Added [CODE][/CODE] tags to preserve spacing
 
Old 03-27-2012, 03:36 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Maybe something like this?
Code:
sed 's/[^ ]*@[^ ]* //; s/&/\t/g' file
May a suggest a good reference to learn sed? Here it is: http://www.grymoire.com/Unix/Sed.html. Hope this helps.
 
1 members found this post helpful.
Old 03-27-2012, 03:55 AM   #3
arn2025
LQ Newbie
 
Registered: Feb 2012
Posts: 26
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
how do i add titles to the top of each of these columns
 
Old 03-27-2012, 06:46 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by arn2025 View Post
how do i add titles to the top of each of these columns
You have different options:

1. Insert text before the first line using sed:
Code:
sed '1i text' file
2. Add a line at the top of the file using awk:
Code:
awk 'NR == 1{print text}1' file
3. Concatenate a line of text and the content of the file using process substitution in bash:
Code:
cat <(echo text) file
The first two options let you edit the file in place, using option -i in sed or redirecting the print statement to the internal variable FILENAME in awk, e.g.
Code:
sed -i '1i text' file
Code:
awk 'NR == 1{print text > FILENAME}1' file
HTH
 
Old 03-27-2012, 11:48 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
It looks like it's time to learn how to use your tools.

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/
 
  


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
Need help manipulating a text file lt1776 Linux - Newbie 4 02-16-2010 12:11 PM
manipulating text in python suran Programming 4 12-01-2009 12:33 AM
Manipulating text phillipseamore Linux - Newbie 6 10-05-2008 01:51 PM
Editing/manipulating text files winchester169 Linux - Software 3 08-18-2005 07:01 PM


All times are GMT -5. The time now is 01:50 PM.

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