views:

89

answers:

3

I'd like to be able to sort a file but only at a certain line and below. From the manual sort isn't able to parse content so I'll need a second utility to do this. read? or awk possibly? Here's the file I'd like to be able to sort:

tar --exclude-from=$EXCLUDE_FILE --exclude=$BACKDEST/$PC-* \
-cvpzf $BACKDEST/$BACKUPNAME.tar.gz \
/etc/X11/xorg.conf \
/etc/X11/xorg.conf.1 \
/etc/fonts/conf.avail.1 \
/etc/fonts/conf.avail/60-liberation.conf \

So for this case, I'd like to begin sorting on line three. I'm thinking I'm going to have to do a function to be able to do this something like

cat backup.sh | while read LINE; do echo $LINE | sort; done

Pretty new to this and the script looks like it's missing something. Also, not sure how to begin at a certain line number.

Any ideas?

+1  A: 

clumsy way:

len=$(cat FILE | wc -l)
sortable_len=$((len-3))

head -3 FILE > OUT
tail -$sortable_len FILE | sort >> OUT

I'm sure someone will post an elegant 1-liner shortly.

Steve B.
+3  A: 

Something like this?

(head -n 2 backup.sh; tail -n +3 backup.sh | sort) > backup-sorted.sh

You may have to fixup the last line of the input... it probably doesn't have the trailing \ for the line continuation, so you might have a broken 'backup-sorted.sh' if you just do the above.

You might want to consider using tar's --files-from (or -T) option, and having the sorted list of files in a data file instead of the script itself.

retracile
tail has a +4 ... ahhhhh.
Steve B.
Yeah, though after re-reading the question, you'd want -n +3. But I suspect that using --files-from would be a better approach for you.
retracile
I discovered this can be trimmed down a bit following Steve's suggestion and doing: 'tail -n +3 backup.sh | sort -o backup.sh'. Doing --files-from=... is a lot better option though, wasn't aware that was available, thank you.
A: 

Using awk:

awk '{ if ( NR > 2 ) { print $0 } }' file.txt | sort

NR is a built-in awk variable and contains the current record/line number. It starts at 1.

Al