Basically, I need the first line moved to the end. I don't care whether it's in bash, awk, sed, perl, or whatever scripting language as long as it works.
Why do you want to load the entire file into memory ( almost entire except for the first line ). Won't that be enough to store the first line and print the rest as and when its being read?
That does what I need it to, but it outputs the results to the console, whereas I need it to output it back to the file. I tried setting it equal to a variable and outputting that to a file, but that output the contents of the file separated by spaces rather than line breaks.
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.
Its better to get various forms of getting it to work, as the saying goes " many ways to skin a cat " ( no uuoc here )
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.
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).
I have to agree. I any case to know the purpose of, and have some concept of the effect of the manipulation of IFS is something that a new scripter would be well advised to learn. It can make some tasks truly trivial.
When manipulating records with shell it is good practice to never put the record into a shell variable. We've had examples on this board where the data has changed because the record contained characters recognised by the shell (e.g. reverse solidus).
Obviously with known valid data there is no problem.
Useful for small files, the simple editor, ed, has a move command:
#!/bin/bash -
# @(#) s1 Demonstrate editing file in place with script-able line editor, ed.
echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) ed
set -o nounset
echo
FILE=${1-data1}
cp original $FILE
echo " Data file $FILE:"
cat $FILE
echo
echo " Messages from ed:"
ed $FILE <<EOF
1m$
w
q
EOF
echo
echo " Modified data file $FILE:"
cat $FILE
exit 0
Producing:
% ./s1
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash
GNU ed version 0.2
Data file data1:
/home/user/Music/file1.mp3
/home/user/Music/file2.mp3
/home/user/Music/file3.mp3
/home/user/Music/file5.txt
/home/user/Music/file7.wav
/home/user/Music/file11.ogg
/home/user/Music/file13.m4a
Messages from ed:
191
191
Modified data file data1:
/home/user/Music/file2.mp3
/home/user/Music/file3.mp3
/home/user/Music/file5.txt
/home/user/Music/file7.wav
/home/user/Music/file11.ogg
/home/user/Music/file13.m4a
/home/user/Music/file1.mp3
See man ed for details on the editing commands ... cheers, drl