Delete first 10 Lines From similar files

The files are named file00 through file99 (hundred files). They are all in the same directory. How do I grab all the hundred files and delete the first 10 lines from each file? I am using CentOS 7.

Replay

Using a simple loop in bash (or ksh93):

for f in file[0-9][0-9]; do
  sed -i '.bak' '1,10d' "$f"
done

The sed editing script 1,10d will delete lines 1 through to 10 from the input stream. The other lines will be outputted. With -i '.bak' we ask sed to do the editing in-line in the file, but to back up the original with a .bak suffix.

Alternatives with explicit looping (allows for sub-intervals):

for (( i = 0; i < 100; ++i )); do
  f="$( printf "file%02d" "$i" )"
  sed -i '.bak' '1,10d' "$f"
done

Using ksh93, another way:

typeset -Z 2 j
for (( i = 0; i < 100; ++i )); do
  j=$i
  sed -i '.bak' '1,10d' "file$j"
done

The typeset -Z 2 j in will make $j expand to a zero-filled number of width 2.

This will delete the first ten lines from all files whose names range from file00 to file99:

sed -i.bak -n '11,$p' file[0-9][0-9]

This might require GNU sed, which is known as gsed on OSX. For other seds, try:

find file[0-9][0-9] -maxdepth 0 -type f -exec sed -i.bak -n '11,$p' {} \;

How it works:

  1. -i.bak tells sed to modify the files in-place, creating a backup file with the .bak extension
  2. -n tells sed not to print any lines unless we explicitly ask it to.
  3. 11,$p tells sed to print lines starting with line number 11 and continuing to the last line (denoted $) in the file.
  4. file[0-9][0-9] is a shell glob that will expand to match all files whose names consist of file followed by two digits.

Alternative

The logic can be inverted. Instead of explicitly printing the lines 11 and after, we could (hat tip: JigglyNaga) delete the first ten lines:

sed -i.bak '1,10d' file[0-9][0-9]

If you prefer a bash loop:

for f in file[0-9][0-9]; do
    tail -n +11 "$f" > tmp
    mv -f tmp "$f"
done

Category: find Time: 2016-07-31 Views: 0
Tags: find

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.227 (s). 12 q(s)