I want to mvoe lines around in a file.
Say I have 30 lines in a file and want to move the last 5 lines to the top of the file..how can this be done?
i thought of awk and sed but was not sure context.
please assist
thanks
awk ' FNR < 6 {arr[FNR]=$0}
FNR > 5 { print $0}
END {for(i=1;i<6;i++) {print arr} } ' filename
One way.
line_move
if [ $# -ne 4];then
echo "Usage: line_move file_to_be_moved start_line_number end_line_number new_position"
fi
file=$1
start=$2
end=$3
position=$4
sed -n "${start},${end}p" ${file} > ${file}.tmp
sed -e "${start},${end}d" -e "${position}r ${file}.tmp" ${file}
no good...neither code worked...
here is the contents of the file
15.5
15.8
16.05
16.29
16.14
16.25
16.34
16.4
15.9
15.3
14.7
14.2
13.3
12.6
12.1
11.7
11.3
11.5
11.7
11.9
23.4
15.5
15.5
15.5
nawk '
{ a[FNR]=$0}
END {
for(i=FNR-4;i<=FNR;i++)
print a
for(i=1;i<=FNR-5;i++)
print a
}' myFile
Hi.
The interactive line editor ed can be driven by a script. Using a data file of the NATA alphabet (line numbers added for the display):
#!/usr/bin/env bash
# @(#) s1 Demonstrate moving data inside file with editor "ed".
echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) edges ed
set -o nounset
echo
FILE=${1-data1}
cp ${FILE}.orig $FILE
echo " Data file $FILE top and bottom:"
edges -l 3 $FILE
echo
echo " Results:"
ed $FILE <<'EOF'
$-4,$m0
w
q
EOF
edges $FILE
exit 0
Producing:
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0
GNU bash 3.2.39
edges (local) 307
GNU Ed 0.7
Data file data1 top and bottom:
1 Alpha
2 Bravo
3 Charlie
...
24 Xray
25 Yankee
26 Zulu
Results:
164
164
1 Victor
2 Whiskey
3 Xray
4 Yankee
5 Zulu
...
22 Quebec
23 Romeo
24 Sierra
25 Tango
26 Uniform
See man ed for details ... cheers, drl