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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-12-2012, 03:24 AM
|
#1
|
|
LQ Newbie
Registered: Aug 2012
Posts: 12
Rep: 
|
start the line only with numbers
hi,
I need some unix command to replace the following thing. The line shuld start with oly numbers. If it starts with anything other than number it shuld be taken back to the last line.
My file:
1234|test
weye|test1|break
576|test|break|title
2369|test|line|break
tite|break
234589|test|like|break
Output should be like below:
1234|testweye|test1|break
576|test|break|title
2369|test|line|breaktite|break
234589|test|like|break
|
|
|
|
08-12-2012, 03:38 AM
|
#2
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
My apologies. I misread the output file, and asked an erroneous question.
Last edited by jschiwal; 08-13-2012 at 06:42 AM.
|
|
|
|
08-12-2012, 04:01 AM
|
#3
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
This is the closest I come without any more explanation:
Code:
tr '\n' '|' < infile | sed -e 's/|\([0-9][0-9]*\)/\n\1/g' -e 's/|$/\n/'
This does add an extra | when lines are appended.
Example:
Code:
$ cat infile
1234|test
weye|test1|break
576|test|break|title
2369|test|line|break
tite|break
234589|test|like|break
$ tr '\n' '|' < infile | sed -e 's/|\([0-9][0-9]*\)/\n\1/g' -e 's/|$/\n/'
1234|test|weye|test1|break
576|test|break|title
2369|test|line|break|tite|break
234589|test|like|break
Not too elegant, but it seems to be doing the job.
|
|
|
|
08-12-2012, 04:08 AM
|
#4
|
|
Member
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 577
|
Hi.
Here is another sed solution:
Code:
$ cat infile
1234|test
weye|test1|break
576|test|break|title
2369|test|line|break
tite|break
234589|test|like|break
$ sed -n '/^[0-9]/{:a; N; /\n[0-9]/be; s/\n//; ba}; :e;p' infile
1234|testweye|test1|break
576|test|break|title
2369|test|line|breaktite|break
234589|test|like|break
The idea is almost the same as here.
Last edited by firstfire; 08-12-2012 at 04:12 AM.
|
|
|
|
08-12-2012, 05:51 AM
|
#5
|
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 563
Rep: 
|
nevermind
Hi,
A grep solution
Code:
bash-4.2$ tr '\n' '|' < infile |grep -Eo "[[:digit:]]*[[:alpha:]|]*"
1234|test|weye|test
1|break|
576|test|break|title|
2369|test|line|break|tite|break|
234589|test|like|break|
Last edited by whizje; 08-12-2012 at 06:05 AM.
|
|
|
|
08-12-2012, 06:59 AM
|
#6
|
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 563
Rep: 
|
Repaired grep
Code:
bash-4.2$ tr '\n' '|' < infile |grep -Eo "[[:digit:]]*([|]+[[:digit:]]*[[:alpha:]]+[[:digit:]]*)*"
1234|test|weye|test1|break
576|test|break|title
2369|test|line|break|tite|break
234589|test|like|break
Last edited by whizje; 08-12-2012 at 07:06 AM.
|
|
|
|
08-12-2012, 11:13 PM
|
#7
|
|
LQ Newbie
Registered: Aug 2012
Posts: 12
Original Poster
Rep: 
|
HI,
When i use
1. sed -n '/^[0-9]/{:a; N; /\n[0-9]/be; s/\n//; ba}; :e;p' infile
i am getting Label to long error.
2. tr '\n' '|' < infile | sed -e 's/|\([0-9][0-9]*\)/\n\1/g' -e 's/|$/\n/' > outfile
0 byte file is received.
Please help me.
Last edited by anshaa; 08-12-2012 at 11:25 PM.
|
|
|
|
08-12-2012, 11:25 PM
|
#8
|
|
LQ Newbie
Registered: Aug 2012
Posts: 12
Original Poster
Rep: 
|
sed -n '/^[0-9]/{:a; N; /\n[0-9]/be; s/\n//; ba}; :e;p' infile
i am getting Label to long error.
|
|
|
|
08-13-2012, 02:34 AM
|
#9
|
|
LQ Newbie
Registered: Aug 2012
Posts: 12
Original Poster
Rep: 
|
nawk 'END{print RS} /^[0-9]/{if(NR>1)print RS}1' ORS= filename > filename1
Works perfectly.
|
|
|
|
08-13-2012, 03:11 AM
|
#10
|
|
Member
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 577
|
Hi.
You probably have an ancient version of sed.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:17 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|