LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-09-2008, 11:35 PM   #1
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Rep: Reputation: 15
moving line above it


Hi guys,

Could anyone suggest how to move a line above it in a large text file, for instance:

input:
----------
link=abc1
uid=S2

link=abc2
uid=S3

link=abc3
uid=S4

output looking for:
-------------------
uid=S2,link=abc1
uid=S3,link=abc2
uid=S4,link=abc3


any ideas appreciated.
 
Old 07-09-2008, 11:55 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
perl -pe '$/ = undef; $_=<>; s/(link=\w+)\n(uid=\w+)\n/\2,\1/sg' FILE

Last edited by Mr. C.; 07-09-2008 at 11:58 PM.
 
Old 07-09-2008, 11:56 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
sed -n '/link/{h;n;G;s/\n/,/p}' filename > newfilename
Let us know if you would like the above translated.

Best SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
 
Old 07-09-2008, 11:58 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by Mr. C. View Post
perl -pe '$/ = undef; $_=<>; s/(link=\w+)\n(uid=\w+)\n/\1,\2/sg' FILE
And I thought SED was inscrutable......

You were faster, but my code is shorter.....
 
Old 07-09-2008, 11:59 PM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Nice, I haven't seen anyone use sed like that in a while!

Now, use sed to solve the palindrome problem!
 
Old 07-10-2008, 01:09 AM   #6
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mr. C. View Post
perl -pe '$/ = undef; $_=<>; s/(link=\w+)\n(uid=\w+)\n/\2,\1/sg' FILE
Thanks guys, I have tested works great. I had some garbage in my text it looks like that's where I got stuck but manually fixed those...for now it does the job well. Appreciated everyone.
 
Old 07-10-2008, 01:18 AM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
pixellany is a god!
 
Old 07-10-2008, 02:26 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'BEGIN{RS="";OFS=","}{print $2,$1}' file
 
Old 12-18-2008, 03:36 PM   #9
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
line move not working, please help

Hello guys,

I tried awk,sed,perl versions above but none did work for the desired output.

Could anyone help please?

Thanks much


input:

dn: uid=ABC,ou=htec,o=cm.com
cquestion: What's your e-mail?
dn: uid=BBB,ou=htec,o=cm.com
cquestion: What's your e-mail?
dn: uid=CCC,ou=htec,o=cm.com
cquestion: What's yout pet's name?
dn: uid=DDD,ou=htec,o=cm.com
cquestion: What's your e-mail?
dn: uid=EEE,ou=htec,o=cm.com
cquestion: What's last name?

output:

dn: uid=ABC,ou=htec,o=cm.com,cquestion: What's your e-mail?
dn: uid=BBB,ou=htec,o=cm.com,cquestion: What's your e-mail?
dn: uid=CCC,ou=htec,o=cm.com,cquestion: What's yout pet's name?
dn: uid=DDD,ou=htec,o=cm.com,cquestion: What's your e-mail?
dn: uid=EEE,ou=htec,o=cm.com,cquestion: What's last name?
 
Old 12-18-2008, 03:56 PM   #10
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by cmontr View Post
Hello guys,

I tried awk,sed,perl versions above but none did work for the desired output.

Could anyone help please?

Thanks much


input:

dn: uid=ABC,ou=htec,o=cm.com
cquestion: What's your e-mail?
dn: uid=BBB,ou=htec,o=cm.com
cquestion: What's your e-mail?
dn: uid=CCC,ou=htec,o=cm.com
cquestion: What's yout pet's name?
dn: uid=DDD,ou=htec,o=cm.com
cquestion: What's your e-mail?
dn: uid=EEE,ou=htec,o=cm.com
cquestion: What's last name?

output:

dn: uid=ABC,ou=htec,o=cm.com,cquestion: What's your e-mail?
dn: uid=BBB,ou=htec,o=cm.com,cquestion: What's your e-mail?
dn: uid=CCC,ou=htec,o=cm.com,cquestion: What's yout pet's name?
dn: uid=DDD,ou=htec,o=cm.com,cquestion: What's your e-mail?
dn: uid=EEE,ou=htec,o=cm.com,cquestion: What's last name?

I tried this awk:
This worked a bit ok but needed to put comma in between and remove the dn: from each line ... Could anyone help to edit?

awk '{
if (NR%2==1)
str=$0
if (NR%2==0)
print NR/2" "str" "$0
}' FILEIN > FILEOUT
 
Old 12-18-2008, 04:10 PM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
I tried awk,sed,perl versions above but none did work for the desired output.
You've been trying since July???.....

When you say that something does not work, it's best to describe exactly what happens or does not happen.

The code I gave you works for the original example--to wit: put the second line in front of the first, 4th in front of the third, etc.

Your current example (2nd line after the 1st) should also be pretty straightforward with SED---it will be a relatively small change to what I had before.
 
Old 12-18-2008, 04:55 PM   #12
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
You've been trying since July???.....

When you say that something does not work, it's best to describe exactly what happens or does not happen.

The code I gave you works for the original example--to wit: put the second line in front of the first, 4th in front of the third, etc.

Your current example (2nd line after the 1st) should also be pretty straightforward with SED---it will be a relatively small change to what I had before.
Hi - No, it was working for that file ...I was working on something new.... Pls give me sometime; I will retest it....
 
Old 12-18-2008, 07:38 PM   #13
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
You have all the time you want........
 
Old 12-18-2008, 08:25 PM   #14
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Exclamation

Quote:
Originally Posted by cmontr View Post
Hi - No, it was working for that file ...I was working on something new.... Pls give me sometime; I will retest it....
Hi Again ... Using sed I get this error:

sed: command garbled: /cquestion/{h;n;G;s/\n/,/p}

perl was working for a smaller file... but for this one that about 250k lines...doing nothing but printing the same file.....

can you please help ?

example:

INPUT:
dn: uid=AAAA,ou=abcd,o=abc.com
cquestion: Where is your favorite vacation spot?

dn: uid=BBBB,ou=abcd,o=abc.com
cquestion: Where is your favorite colour?

dn: uid=CCCC,ou=abcd,o=abc.com
cquestion: What is your last name?

ouput should be like:

dn: uid=AAAA,ou=abcd,o=abc.com,cquestion: Where is your favorite vacation spot?
dn: uid=BBBB,ou=abcd,o=abc.com,cquestion: Where is your favorite colour?
dn: uid=CCCC,ou=abcd,o=abc.com,cquestion: Where is your last name?

.
.
.

these are about 250k lines....

thank you very much...
 
Old 12-18-2008, 08:52 PM   #15
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Hi Again ... Using sed I get this error:

sed: command garbled: /cquestion/{h;n;G;s/\n/,/p}
You would need to post the complete command you used.

BUT---that command I gave you is not what you want now (because you now want things in a different order)

First, let's look at what the original does:
Quote:
sed -n '/link/{h;n;G;s/\n/,/p}' filename > newfilename
Translation:
-n (suppress printing)
/link/ (for lines containing "link")
h (copy the line to the hold register)
n (get the next line)
G (recall the line from the hold register and append it to what you just read)
<<at this point we have detected the line containing "link", gotten the next line, and then added the first one after it>>
s/\n/,/ (replace the newline char. with a ",")
p (print out the final result)

In your most recent example, you want to key on the line containing "dn", and then add the next line after. The code will be similar, but, instead of appending from the hold register to the working register (G), you'll want to append to the hold register (H)

Go all the way back to my original post and look at the link--it explains all this quite well.

Last edited by pixellany; 12-18-2008 at 08:53 PM.
 
  


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
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
Moving files from the command line adam-red Linux - Newbie 5 12-06-2004 04:42 AM
copying/moving stalls when moving a lot of data to a usb stick =X¥®µ§= Linux - Hardware 10 07-30-2004 05:29 AM
moving files and folders thru command line JROCK1980 Linux - General 2 02-16-2004 06:55 PM

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

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