简体   繁体   中英

How can I write to a specific line range with sed or awk?

I simply want to write a command's output to a file's particular line range.

For instance first command should be written into range 0-1000 then second command into 1001-2000 etc.

I've successfully managed to write to a single line with sed -i command which doesn't help me at all.

What I lastly tried in a for loop is;

    for cmd in "${commands[@]}"; do
        awk "NR >=$counter && NR <=$((counter + 1002)) {print $(eval $cmd)}" file > $logfile
        counter=$((counter + 1003))
    done

which throws argument is too long error.

Any help would be appreciated.

Instead of evaluating the same command and incrementing counter on thousands of awk iterations - use the following sed optimized approach:

cnt=1
for cmd in "${commands[@]}"; do
    cmd_out="$(eval $cmd)"
    sed -n "$cnt,$((cnt + 1002)) s/.*/$cmd_out/p" 10lines.txt >> $logfile
    cnt=$((cnt + 1003))
done

But if don't actually use the contents of the processed file - you can just iterate through inner ranges and print/append the same command output to a destination file.

How about something like this? The awk script either truncates or pads to 1000 lines.

$ cat foo.sh
for cmd in 'seq 5' 'seq 3000'; do
        $cmd | awk 'NR > 1000 { exit } END { while (NR++ < 1000) print ""} 1'
done >foo.txt
$ bash foo.sh
$ wc -l foo.txt
2000 foo.txt
$ head foo.txt
1
2
3
4
5





$ tail foo.txt
991
992
993
994
995
996
997
998
999
1000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM