Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Join line containing pattern to preceding line

Join line containing pattern to preceding line

Join line containing pattern to preceding line


I have a large file containing lots of data, however, I want to do the following;

Find any lines containing the pattern cn=

and join this line to the preceeding line (same as shift J in vi)- if that makes sense?

RE: Join line containing pattern to preceding line

Minor fix: add a space before the %s:


#--- Here ---V
#...{printf " %s"... 

The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

RE: Join line containing pattern to preceding line


Thanks for this, and it works great.

However, now I need to put this into a perl script & I am having issues with getting it to work. I have tried backticks, qq etc. and no good. Any ideas how to make this work within a perl script?

I run this ->

my $tmpfile_2 = qq|/tmp/somefile2.bak|;
my $awk_cn_command = qq|awk 'NR>1 && !/cn=/{printf '"\n"'}{printf '"%s"',$0}END{printf '"\n"'}' $logout > tmpfile_2|;
print $awk_cn_command;

and the output I get is;

awk 'NR>1 && !/cn=/{printf '"
"'}{printf '"%s"',./login_errors.pl}END{printf '"
"'}' /tmp/failure.log > tmpfile_2awk: syntax error near line 2
awk: illegal statement near line 2

so the /n is breaking it for some reason...

RE: Join line containing pattern to preceding line

Figured out the issue; needed to be;

my $awk_cn_command = qq|awk 'NR>1 && !/cn=/{printf "\\n"}{printf "%s",\$0}END{printf "\\n"}' $tmpfile_1 > $tmpfile_2|;

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close