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
# 8  
Old 10-16-2013
Quote:
Originally Posted by ctsgnb
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
Code:
#!/bin/ksh
infile="Input"
outfile="Output"
ed -s "$infile" <<-EOF
        /Output view:/m0
        w $outfile
        q
EOF

This was tested using the Korn shell, but will work with any shell that accepts basic Bourne shell syntax.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 10-16-2013
Hi,

May be this code can help too.

Code:
$ awk '
BEGIN{
print "Output view:"
} NR==4 || /Output/ {next} {print $1}' file_name

Output will be as follows.

Code:
Output view:
AAA
BBBB
CCCC
XXXX
YYYY
ZZZZ



Thanks,
R. Singh
# 10  
Old 10-17-2013
Quote:
Originally Posted by shamrock
Yet another way with the trusty ole ex...
Code:
ex -s +'/^Output view:/m0 | x' file

Nice.

I found this about the use of + :
Quote:
The -c replacement for + command was inspired by the -e option of sed. Historically, all such commands (see edit and next as well) were executed from the last line of the edit buffer. This meant, for example, that "+/pattern" would fail unless the wrapscan option was set. POSIX.1-2008 requires conformance to historical practice. The + command option is no longer specified by POSIX.1-2008 but may be present in some implementations. Historically, some implementations restricted the ex commands that could be listed as part of the command line arguments. For consistency, POSIX.1-2008 does not permit these restrictions.

ex: Rationale - options

This seems to work too:
Code:
ex -sc '/^Output view:/m0 | x' file

This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 10-17-2013
One more lengthy way

may try

Code:
$ awk 'BEGIN{while(1){getline;if(/Output/){printf $0;break}x=x RS $0}print x}1'  file

OR
Code:
$ awk '{if(/Output/)printf $0;else x=x RS $0}END{print x}' file

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


Last edited by Akshay Hegde; 10-17-2013 at 03:54 AM..
This User Gave Thanks to Akshay Hegde For This Post:
# 12  
Old 10-17-2013
Code:
sed -e '/^Output view:/{G;p;}' -e '1{h;d;}' -e '2,/^Output view:/{H;d;}' file

maybe better readable as
Code:
sed '
/^Output view:/{G;p;}
1{h;d;}
2,/^Output view:/{H;d;}
' file

But sed is hacking; the string must be mentioned twice. awk is better:
Code:
awk 'p; /^Output view:/ {print; print s; p=1} !p {s=s sep $0; sep=RS}' file

# 13  
Old 10-28-2013
sir,

code
awk '/Output view:/{f=1; $0=$0 p; p=x} !f{p=p RS $0} f' file

works fine. But can you explain me step by step by step how it works.
# 14  
Old 10-28-2013
Quote:
Originally Posted by paresh n doshi
sir,

code
awk '/Output view:/{f=1; $0=$0 p; p=x} !f{p=p RS $0} f' file

works fine. But can you explain me step by step by step how it works.
Reformatting Scrutinizer's code and adding comments:
Code:
awk '           # invoke awk program and start script to be run by awk
/Output view:/ {# When an input line contains the string "Output view:"...
        f=1             # set f (found) to 1,
        $0=$0 p         # append the contents of p to the end of the
                        # current input line, and 
        p=x             # set p to the contents of x (an empty string) 
}       
!f {            # If f is 0...
        p=p RS $0       # append the record separator (default value is
                        # the newline character) and the current input
                        # line to p
}       
f               $ If f is not 0, print the current input line.
' file          # End awk script and specify the name of the input file.

These 3 Users Gave Thanks to Don Cragun 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