Script to move the first line of a file to the end


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to move the first line of a file to the end
# 8  
Old 12-27-2008
Quote:
Originally Posted by Altay_H
Code:
#!/bin/bash
var=`awk 'NR==1{store=$0;next}1;END{print store}' list.txt`
echo $var > list.txt

What about:
Code:
echo "$var" > list.txt

# 9  
Old 12-29-2008
Or use "sed" to copy lines 2 onwards, then get the first line with "head -1".

#!/bin/ksh
cat list.txt | sed -n "2,\$ p" >new_list.txt
head -1 list.txt >> new_list.txt
# 10  
Old 12-29-2008
Quote:
cat list.txt | sed -n "2,\$ p" >new_list.txt
This is UUOC, cat is not needed here

just sed '<operation>' filename would do

Last edited by vgersh99; 12-29-2008 at 01:34 PM.. Reason: url for UUOC
# 11  
Old 12-29-2008
Quote:
Originally Posted by matrixmadhan
This is UUOC, cat is not needed here

just sed '<operation>' filename would do

Nor is head required:

Code:
IFS= read -r line < list.txt
printf "%s\n" "$line" >> new_list.txt

# 12  
Old 12-30-2008
It is possible to write scripts which might work efficiently but are unintelligible to newbies. The previous post with "IFS" is a classic.
We may post examples which are easy to follow rather than what we might use in a commercial environment. Most people find left-to-right date flow easier to follow.

IMHO the best outline design is the compact "awk" program which the poster tried but got the wrong results. It needs adjusting to not put the whole file into a variable called $var, but to write directly to the new file instead. This could be done from "awk" itself, but here is a way with shell redirect.

awk 'NR==1{store=$0;next}1;END{print store}' list.txt >new_list.txt

In a production script you would probably then copy "list.txt" before overwriting it with "new_list.txt" .

Last edited by methyl; 12-30-2008 at 01:17 PM..
# 13  
Old 12-31-2008
Quote:
It is possible to write scripts which might work efficiently but are unintelligible to newbies
Its better to get various forms of getting it to work, as the saying goes " many ways to skin a cat " ( no uuoc here Smilie )

Initially for a newbie it might be of interest to get the task done and later it might be interesting and a good learning experience to make it better, optimal without having to spawn too many processes where it could be achieved by shell built-ins etc.
# 14  
Old 12-31-2008
I fully agree with matrixmadham. Whether on unix , DOS, VME or whatever building shell programs is best learnt step-by-step with prototype scripts and examples which can then be improved with experience.
Each year we will find a move towards a fashionable standard. Whatever happened to OS-9 and DCL as universal scripting languages? We are learning from cfajohnson of a move towards Posix-2 , but to get "normal" Berkeley behaviour from commands such as "ps" and "xd" we often need to go outside Posix-2 and even the less-strict XPG4 standard.

When we work on multiple unix/Linux/etc. platforms it is useful to learn portable scripting techniques and to be aware of useful commands that are not always implemented like Linux "stat", or can have differerent syntax like "echo", or can have a mishmash of variations like "awk" and "nawk".

IMHO we will still be debating standards up to 18/01/2038 (the Y2K38 problem).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

3. Shell Programming and Scripting

Cannot execute/finish script because of last line syntax error: unexpected end of file/token `done'

first of all I thought the argument DONE is necessary for all scripts that have or begin with do statements which I have on my script, However, I still don't completely understand why I am receiving an error I tried adding another done argument statement but didn't do any good. I appreciate... (3 Replies)
Discussion started by: wolf@=NK
3 Replies

4. Shell Programming and Scripting

sed: how to move matched pattern to end of previous line

Hello, I'm new to this forum. I've been doing a lot of sed work lately and have found many useful tips on this forum. I've hit a roadblock in a project, though, and could really use some help. I have a text file with many lines like the following, i.e., some lines begin with a single word... (3 Replies)
Discussion started by: paroikoi
3 Replies

5. Shell Programming and Scripting

how to move a word to the end of file

Hey guys, I want move a specific word from the middle of the text and move it the end of the file, which means I want the word to be deleted from it's line and moved to the end of file. I know how to use sed for adding a word the end of file, but I don't know how to move words. tnx (2 Replies)
Discussion started by: Johanni
2 Replies

6. Shell Programming and Scripting

help needed with shell script to append to the end of a specific line in a file on multiple servers

Hi Folks, I was given a task to append three IP's at the end of a specific (and unique) line within a file on multiple servers. I was not able to do that with the help of a script. All I could was: for i in server1 server2 server3 server4 do ssh $i done I know 'sed' could be used to... (5 Replies)
Discussion started by: momin
5 Replies

7. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

8. 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

9. Shell Programming and Scripting

script to move two lines to the end of a file

My input file is multiline file and I am writing a script to search for a pattern and move the line with the pattern and the next line to the end of the file. Since I am trying to learn awk, I thought I would try it. My input looks like the following: D #testpoint 1 510.0 D #testpoint2 ... (5 Replies)
Discussion started by: banjo25
5 Replies

10. Shell Programming and Scripting

Help on shell script : syntax error at line 62: `end of file' unexpected

Hi All, I have written a korn script (code pasted below). It is giving the error while debugging "new.sh: syntax error at line 62: `end of file' unexpected". I have re-written the whole code in VI and explored all help related to this error on this Unix forum and tried it. Somehow, I could... (7 Replies)
Discussion started by: schandrakar1
7 Replies
Login or Register to Ask a Question