简体   繁体   中英

How to remove from all files in folder the lines with specific string

I'm looking for the way to remove all lines from file (for all files in folder) have string # and also copy the name of file and add it as first column (or last column):

so as example I have:

# DX 
# DEPTH 
MD   DX  DY
100  23  35
100  23  35

output should be like this, let's say file name is test2:

test2 MD   DX  DY
test2 100  23  35
test2 100  23  35

thanks, S

Utilizing simple bash structures, iterate through each line of each file, save the line and file name as variables, then per line in file, print both variables together.

for file in *; do
  for (( i = 1 ; i <= $( grep "" -c $file ) ; i++ ); do
    line=$( cat $file | head -n ${i} | tail -1 )
    echo "${file} ${line}"
  done
done

Considering using find/sed to construct the command line for each process.

find * -type f | xargs -I@ sed -e '/#/d' -e 's/^/@ /' @

The above will print the combined output. If you need to modify the files, consider using inline edit (sed -i)

find * -type f | xargs -I@ sed -i -e '/#/d' -e 's/^/@ /' @

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