Move a line to top of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Move a line to top of the file
# 1  
Old 10-16-2013
Move a line to top of the file

Hi,
I have a following file and it has only one occurrence of line that says "Output view:". It could be in middle somewhere ( i don't know the exact location ). I want to move it as the first line of the file.

Input
Code:
AAA
BBBB
CCCC
Output view:
XXXX
YYYY
ZZZZ

Output should be:
Code:
Output view:
AAA
BBBB
CCCC
XXXX
YYYY
ZZZZ

# 2  
Old 10-16-2013
Code:
sed '/Output view:/d;1i\Output view:' file

Emanuele
# 3  
Old 10-16-2013
Lazy (but easy to read) solution...
Code:
grep "Output view:" input >output
grep -v "Output view:" input >>output

But no doubt it must be some more elegant solution ...

@Alister :
Any "ed" suggestion ? Smilie

Last edited by ctsgnb; 10-16-2013 at 10:53 AM..
# 4  
Old 10-16-2013
Hi,
Another sed solution (also work with regular expression):
Code:
 sed -n '1,/Output view:/{/Output view:/{G;s/\n//p;b;};H;b};p' file

Regards.
# 5  
Old 10-16-2013
Thanks guys!
# 6  
Old 10-16-2013
Yet another way with the trusty ole ex...
Code:
ex -s +'/^Output view:/m0 | x' file

This User Gave Thanks to shamrock For This Post:
# 7  
Old 10-16-2013
Two more:
Code:
awk '/Output view:/{f=1; $0=$0 p; p=x} !f{p=p RS $0} f' file

or perhaps simply
Code:
awk 'NR==1{print s} $0!~s' s="Output view:" file

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move a text to next line in a file

Hi , I need your help for the below issue. I have a file which has data as below An error came (/u01/app/12.csv) pkg1.func1: detail s 1111-->pkg1.func1: detail s 2222--> Now pkg1.func1: .... --> can come multiple times in the second line. I need to arrange the data in the below... (9 Replies)
Discussion started by: bhaski2012
9 Replies

2. UNIX for Dummies Questions & Answers

add a new line on top of a file

infile a b c A E F 1 2 3 outfile new line a b c A E F 1 2 3 I tried: sed '1i\ new line' infile > outfilecat outfile new linea b c A E F 1 2 3 I don't want the new line be added to the existing first line. Thanks Joseph (7 Replies)
Discussion started by: jdhahbi
7 Replies

3. Shell Programming and Scripting

Use sed to move last line to top

I have parsed a curl download with sed commands. I would also like to move the last line in the output file to the top. Can I use sed for this? (3 Replies)
Discussion started by: jostber
3 Replies

4. Shell Programming and Scripting

append a line into a file in the top

hi, My code is #!/bin/sh echo "\n\nPlease enter the month of the year(YYYYMM) : \c" read date_rep INPUT_L9_FILE=L9_Recharge_Description_EOM_$date_rep.csv #This part is used to summarise Grand_Total, Balance_Total of file L9_Recharge_Description_EOM_${1}.csv. awk -F"," '{if(NR!=1)... (5 Replies)
Discussion started by: madfox
5 Replies

5. Shell Programming and Scripting

Move a line to end of file

Can somebody help me with a script .... Read a file /etc/inittab find the string starting with rcml and move it entirely towards the end of file. rcml:2:once:/usr/sni/aix52/rc.ml > /dev/console 2>&1 I basically want to change the startup sequence. (2 Replies)
Discussion started by: imanuk2007
2 Replies

6. Shell Programming and Scripting

best way to insert a line at the top of a file?

say I want to insert "this is a test" as the first line into file A, besides echo "this is a test" > /tmp/tmpfile cat /tmp/tmpfile fileA >> /tmp/result, is there any simple way I can do it? thanks (7 Replies)
Discussion started by: fedora
7 Replies

7. AIX

want to remove some line from top of file.

Hi All, I have AIX 5.3 server. I have one big file. in that i want to remove 5000 line from top. is there any command for this? Thanks, Vishal (6 Replies)
Discussion started by: vishalpatel03
6 Replies

8. Shell Programming and Scripting

find top 100 files and move them

i have some 1000 files in my dir and i want to find top 100 files and move them to some other location: below the 2 commands i used, but it is not working ls -ltr | grep ^- | head -100 | xargs mv destination - _________>not working ls -ltr | grep ^- | head -100 | xargs mv {}... (3 Replies)
Discussion started by: ali560045
3 Replies

9. Shell Programming and Scripting

how to move the line after a certain pattern in the file

Hi, I have a file called /bb/bin/rstrt. I need to move the line/entry "ccpm_load_shared_memory" after the entry "initcorp". The problem is that there are several entries for "initcorp" in this file and I need the entry to be moved only after the first instance of "initcorp" Is there a way... (5 Replies)
Discussion started by: aoussenko
5 Replies

10. Programming

how to move file pointer to a particular line in c

Hello experts, I ve a text file I want to go to particular line . what is the best way to do this in c ? I am tried as follows fseek ( fh, pos, SEEK_SET); but this functions moves the file pointer according to a number of bytes. Unfortunately I don't know the exact byte... (7 Replies)
Discussion started by: user_prady
7 Replies
Login or Register to Ask a Question