Find 2 expressions then print in a single line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find 2 expressions then print in a single line
# 1  
Old 12-13-2012
Find 2 expressions then print in a single line

Guys, need help.

I have a file that contains something like this:

Code:
abc
def
ghi
jkl

I want to print the first and last line of the file and the output should be in a single line.

so, output should be like this:
Code:
abc jkl


Last edited by Scott; 12-13-2012 at 11:52 AM.. Reason: Code tags
# 2  
Old 12-13-2012
try:
Code:
awk 'NR==1 {printf $0" "}END {print}' infile

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 12-13-2012
It worked!

Moreover, can I add a pattern on that code?
e.g
3 files

Code:
abc
def
ghi
jkl

Code:
123
456
789
jkl

Code:
abc
def
ghi
123

Can I print only the files that contains 'jkl' on the end?

output:

Code:
abc jkl
123 jkl

Thanks for the quick response

Last edited by Scott; 12-13-2012 at 11:52 AM.. Reason: Code tags
# 4  
Old 12-13-2012
try also:
Code:
awk '$NF ~ /jkl/ {print $1,$NF}' RS= file1 file2 file3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed find multiple expressions plus next next line

Hello I am trying to write sed code where it will look through the text for lines with specific expression "Started by user" and when found, will also add the following line, and also lines with another expression "Finished:" sed -n '/Started by user/, +1p, /Finished:/'... (4 Replies)
Discussion started by: dlesny
4 Replies

2. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

3. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

4. Shell Programming and Scripting

Multiple pattern match and print the output in a single line

I need to match two patterns in a log file and need to get the next line of the one of the pattern (out of two patterns) that is matched, finally need to print these three values in a single line. Sample Log: 2013/06/11 14:29:04 <0999> (725102) Processing batch 02_1231324 2013/06/11... (4 Replies)
Discussion started by: rpm120
4 Replies

5. Shell Programming and Scripting

Print in single line

HI I am having a flle like below . test.txt ssh aaaaaaa@bbbbbbbbb "head -1 /xxxxxx/yyyyyyy/yyyyyyy.sp.issuer_upd_fd" echo /xxxxxx/yyyyyyy/yyyyyyy.sp.issuer_upd_fd ssh aaaaaaa@bbbbbbbbb "head -1 /xxxxxx/yyyyyyy/yyyyyyy.sp.xreff_upd_gm" echo /xxxxxx/yyyyyyy/yyyyyyy.sp.xreff_upd_gm ... (3 Replies)
Discussion started by: ptappeta
3 Replies

6. Shell Programming and Scripting

Print (echo) variable in a single line

Hi, I have written this code ------------------------------------------------ # !/bin/ksh i=0 while do j=$i while do echo -e $j #printf "%d",$j j=`expr $j - 1` done echo i=`expr $i + 1` done ---------------------------------------------------- The ouput which... (2 Replies)
Discussion started by: rac
2 Replies

7. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

8. UNIX for Advanced & Expert Users

What is the difference between single line mode and multiline mode in Regular expressions?

Hi All, Can please let me know what is the difference between the single line mode and multi line mode in regular expresions? Thanks, Chidhambaram B (3 Replies)
Discussion started by: chidhu.anu
3 Replies

9. Shell Programming and Scripting

Find a pattern in a single line

Hi I have to find if a pattern is present in a line. eq line="dasdasd hello asdasdasd" Have to find if "hello" is present in the line or not. Which command to be used? Thanks (10 Replies)
Discussion started by: akashtcs
10 Replies

10. Shell Programming and Scripting

How to print the output in single line

Hi, Please suggest, how to get the output of below script in single line, its giving me in different lines ______________________ #!/bin/ksh export Path="/abc/def/ghi"; Home="/home/psingh/prat"; cd $Path; find $Path -name "*.C#*" -newer "abc.C#1234" -print > $Home cat $Home | while... (1 Reply)
Discussion started by: Prat007
1 Replies
Login or Register to Ask a Question