Line number based manipulation

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Line number based manipulation
# 1  
Line number based manipulation

I have a case where I am grabbing patterns and subsequent lines using

sed -n -e '/regex/{$!N;p;}'

This returns just the regex line when it is the last line of my file.

Now I may have even number of lines in some cases (regex never at end) and odd in very rare cases.

If the line count is odd, then I want to append to this a line from another file.

How do I do this? I dont know how to refer to another file within a awk file processing on someother file.

Moderator's Comments:
Mod Comment Please use code tags!

Last edited by zaxxon; 03-29-2012 at 08:50 AM.. Reason: code tags, see PM
# 2  
Please give an example of :
- what input you have
- the name and content of the file from which you want to extract the line to be merged with your output in the odd case
- what output you expect.
# 3  
I am giving you the simplest example-
After I get regex and the next line, I further get only the columns that I am interested in. So it is something like this-


(this is the case of lines being present after regex), if not the last lines is not present, ie

If I find this has odd number of lines- I want to take input from another file which is input.txt with content
90 xxxxxxxxxxx (I will take only the first column from this) and merge as the fourth line of the file.

is this example ok? lemme know if something is unclear
# 4  
This would add the content of input.txt as a last line, if the line count is odd...
sed '/regex/!d;$!{N;p;d;};r input.txt' infile

Last edited by Scrutinizer; 03-29-2012 at 04:26 PM.. Reason: The last $ was superfluous so I removed it...
# 5  

Thanks a lot. that seems so elegant and works perfectly!
If possible could you give hints on how it works..

Thanks again!

Last edited by Corona688; 03-29-2012 at 04:25 PM.. Reason: delete accidental duplicate
# 6  
Hi, here is a short explanation:
/regex/!d Delete every line that does not match /pattern/ and in that case start processing the next line.
$!{N;p;d;}The rest of the lines match the pattern. For those lines, except when we are on the last line($) do the following: append next line (N) print these two lines and then delete them (d). The d-command also stops processing of the current line, and the cycle for the next line gets started.
r input.txtThe only possibility for arriving here is if there was a pattern match at the very last line. Now we read input.txt and print it.
# 7  
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #505
Difficulty: Medium
An executable statement may not invoke (or call or execute) another procedure (also called subroutine, function, method, etc.),
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort file based on number of delimeters in line

Hi, Need to sort file based on the number of delimeters in the lines. cat testfile /home/oracle/testdb /home /home/oracle/testdb/newdb /home/oracle Here delimeter is "/" expected Output: /home/oracle/testdb/newdb /home/oracle/testdb /home/oracle /home (3 Replies)
Discussion started by: Sumanthsv
3 Replies

2. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

3. Shell Programming and Scripting

How to split a file based on pattern line number?

Hi i have requirement like below M <form_name> sdasadasdMklkM D ...... D ..... M form_name> sdasadasdMklkM D ...... D ..... D ...... D ..... M form_name> sdasadasdMklkM D ...... M form_name> sdasadasdMklkM i want split file based on line number by finding... (10 Replies)
Discussion started by: bhaskar v
10 Replies

4. Shell Programming and Scripting

Splitting a file based on line number

Hi I have a file with over a million lines (rows) and I want to split everything from 500,000 to a million into another file (to make the file smaller). Is there a simple command for this? Thank you Phil (4 Replies)
Discussion started by: phil_heath
4 Replies

5. Shell Programming and Scripting

[Solved] How to separate one line to mutiple line based on certain number of characters?

hi Gurus, I need separate a file which is one huge line to multiple lines based on certain number of charactors. for example: abcdefghi high abaddffdd I want to separate the line to multiple lines for every 4 charactors. the result should be abcd efgh i hi gh a badd ffdd Thanks in... (5 Replies)
Discussion started by: ken6503
5 Replies

6. Shell Programming and Scripting

Delete lines based on line number

I have a file with ~200K lines, I need to delete 4K lines in it. There is no range. I do have the line numbers of the lines which I want to be deleted. I did tried using > cat del.lines sed '510d;12d;219d;......;3999d' file > source del.lines Word too long. I even tried... (2 Replies)
Discussion started by: novice_man
2 Replies

7. Shell Programming and Scripting

Print selection of line based on line number

Hi Unix gurus Basically i am searching for the pattern and getting the line numbers of the grepped pattern. I am trying to print the series of lines from 7 lines before the grepped line number to the grepped line number. I am trying to use the following code. but it is not working. cat... (3 Replies)
Discussion started by: mohanm
3 Replies

8. Shell Programming and Scripting

perform a check based on number of @ in a log line

Hello, I am intending to perform a check based on number of "@" , present in a line in a log file . The idea is basically to perform a check on cc or bcc sender, based on an email log, which shows all the for email address. Say if the number of @ is more than 30, I will consider it as a mass... (12 Replies)
Discussion started by: fed.linuxgossip
12 Replies

9. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies

10. Shell Programming and Scripting

extracting a line based on line number

i want to cut all the entries from the /etc/passwd file in which the uid is> 500 for this i was writing this ,m quiet new to all this.. scripting but on the 6th n 8th line ,, i hav to specify a line number .. to get the commnd working .. but i want to use variable i instead of that ,,... (2 Replies)
Discussion started by: narendra.pant
2 Replies

Featured Tech Videos