LinuxQuestions.org
Visit Jeremy's Blog.
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 04-08-2011, 04:32 PM   #1
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Rep: Reputation: 15
join lines


All,
I have following file which is output of ldapsearch command

Quote:
something:cn=someuser,cn=something,ou=some,ou=somea,
o= some#1#(22982374293742998374893)
something:cn=someotheruser,cn=something,ou=some,ou=s
om ea,o= some#1#(22982374293742998374893)
something:cn=someoneuser,cn=something,ou=some,ou=som
ea ,o= some#1#(22982374293742998374893)
something:cn=someoonetheruser,cn=something,ou=some,o
u= som ea,o= some#1#(22982374293742998374893)
actuvally 2nd, 4th..etc line starts with space.
so I wanted to use grep command in script to get one of those values in file.
Quote:
test=`grep "cn=someuser" "/tmp/test/dir.txt"`
i get only the first line as below
Quote:
something:cn=someuser,cn=something,ou=some,ou=somea,
but I wanted to have in one line
Quote:
something:cn=someuser,cn=something,ou=some,ou=somea,o= some#1#(22982374293742998374893)
also once I get the above value, I wanted to replace the value "#1#" to #0#. How can we do this.
Thanks
 
Old 04-08-2011, 05:14 PM   #2
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
if you do "grep -A1" they'd be concatenated, however there's still this trick that the 2nd lines start with a space, which will be added in the concatenation and hence result in a bad ldap record.

so, you have to use a multi-trap-rocket-science thing:

0) make a backup of your output ;-)
1) remove the leading spaces on each line
Code:
sed -i 's/^ .*/.*/g' ldap_output_file
2) grep -A1 on the cn as you stated above
3) your $test now contains concatenated output, append that to a secondary output
4) apply sed like this sed -i 's/#1#/#0#/g'; however, given that this is ldap, you'll have to move #2# to #1# I guess, but I'm tired, so I am not going into that (yet).


Another approach is to use sed and print only the even lines in "file_even" and the odd lines in "file_odd", number them and join and remove line numbers:
Code:
1) sed -n 1~2p ldap_output | cat -n - > fileA
2) sed -n 2~2p ldap_output | cat -n - > fileB
3) join fileA fileB | cut -d ' ' -f 2- > new_ldap_output_file
4) sed -i "s/#1#/#0#/g' new_ldap_output_file
 
Old 04-08-2011, 06:58 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
You can use tr and sed to merge the continued lines first:
Code:
tr '\a\n' ' \a' < input | sed -e 's|\a[\t ]||g' | tr '\a' '\n' > output
The first tr converts all BELs (\a) to spaces, and newlines (LF AKA \n) to BELs. The resulting output (which is a single long line) is fed to sed, which removes any BELs followed by a space or a tab (HT aka \t). Then, tr is used again to convert any BELs left to newlines. The output is saved to file output. Note that input and output must be different files; if they're the same file, you will just trash it.

Here is a wrapper around ldapsearch, which joins the continued lines together. Save it and run it just as if you would ldapsearch:
Code:
#!/bin/sh
ldapsearch "$@" | tr '\a\n' ' \a' | sed -e 's|\a[\t ]||g' | tr '\a' '\n'
 
Old 04-08-2011, 08:53 PM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
is this what you want?
Code:
$ ruby -ne 'print ($.%2==0)? $_: $_.chomp' file
something:cn=someuser,cn=something,ou=some,ou=somea,o= some#1#(22982374293742998374893)
something:cn=someotheruser,cn=something,ou=some,ou=som ea,o= some#1#(22982374293742998374893)
something:cn=someoneuser,cn=something,ou=some,ou=somea ,o= some#1#(22982374293742998374893)
something:cn=someoonetheruser,cn=something,ou=some,ou= som ea,o= some#1#(22982374293742998374893)

$ awk 'ORS=(NR%2==0)?"\n":"\0"' file
something:cn=someuser,cn=something,ou=some,ou=somea,o= some#1#(22982374293742998374893)
something:cn=someotheruser,cn=something,ou=some,ou=som ea,o= some#1#(22982374293742998374893)
something:cn=someoneuser,cn=something,ou=some,ou=somea ,o= some#1#(22982374293742998374893)
something:cn=someoonetheruser,cn=something,ou=some,ou= som ea,o= some#1#(22982374293742998374893)
 
Old 04-09-2011, 01:31 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Assuming the space needs to be removed (not sure about ruby but kurumi will definitely fix it):
Code:
awk 'ORS=(gsub(/ /,""))?"\n":"\0"' file1
something:cn=someuser,cn=something,ou=some,ou=somea,o=some#1#(22982374293742998374893)
something:cn=someotheruser,cn=something,ou=some,ou=somea,o=some#1#(22982374293742998374893)
something:cn=someoneuser,cn=something,ou=some,ou=somea,o=some#1#(22982374293742998374893)
something:cn=someoonetheruser,cn=something,ou=some,ou=somea,o=some#1#(22982374293742998374893)
And assuming the search option is required, you can use the alternative:
Code:
awk '/cn=someuser/{line=$0;getline;gsub(/ /,"");print line $0}' file
something:cn=someuser,cn=something,ou=some,ou=somea,o=some#1#(22982374293742998374893)
 
Old 04-11-2011, 10:00 AM   #6
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks guys
It worked with sed command and wrapper around ldapsearch.

Thanks a lot.
 
  


Reply

Tags
ldapsearch



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
[SOLVED] Join 5 lines blocks comma delimited hattori.hanzo Programming 8 11-26-2010 09:28 AM
Join lines in text file vidyashankara Linux - General 10 12-21-2009 03:17 PM
join lines until a $ is found powah Programming 2 07-26-2007 03:19 AM
Join all lines using sed chipix Programming 3 04-03-2007 09:55 AM
join every three lines of a text file powah Programming 8 02-01-2007 11:40 PM

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

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