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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
08-12-2012, 04: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, 04:38 AM
|
#2
|
LQ Guru
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 07:42 AM.
|
|
|
08-12-2012, 05:01 AM
|
#3
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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, 05:08 AM
|
#4
|
Member
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709
|
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 05:12 AM.
|
|
|
08-12-2012, 06:51 AM
|
#5
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594
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 07:05 AM.
|
|
|
08-12-2012, 07:59 AM
|
#6
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594
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 08:06 AM.
|
|
|
08-13-2012, 12:13 AM
|
#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-13-2012 at 12:25 AM.
|
|
|
08-13-2012, 12:25 AM
|
#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, 03: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, 04:11 AM
|
#10
|
Member
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709
|
Hi.
You probably have an ancient version of sed.
|
|
|
All times are GMT -5. The time now is 01:15 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
|
|