Looking for accessing and editing multiple files in a directory.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sumit jain
    New Member
    • Oct 2010
    • 1

    Looking for accessing and editing multiple files in a directory.

    #Looking for logic which can modify multiple files in a #directory.Eg /Sumit/Files/ in this there are 3 file #Sam123.12,Sam4 37.34,Sam676.81 .read file one by one and #need only those line which can match pattern like S[1234 #in a file and delete rest lines in file.

    Two things are there:
    read multiple files one by one automatically.
    modify each file with respect to pattern.

    thanks in advance
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    Code:
    #!/bin/bash
    
    directory="/Sumit/Files"
    mkdir output_dir
    
    for f in `ls -1 $directory`
    do
            sed '/S\[1234/d' "$directory/$f" > "output_dir/$f"
    done
    It should create new directory named output_dir in your current directory. It will contain all modified files.

    In sed command specify whatever regex/pattern you want to match, whichever line matches that pattern will not be redirected to new file.

    Comment

    Working...