1

I would like using shell or any script to modify all txt files available in myfolder as follow

 test = data(x, y);

with

test = data(z,x,y);  // by adding z

and :

 test.value(f1, "directory_1/directory_2/directory_3/file");

with

test.value(f1, "/directory_3/file"); // by removing directory_1/directory_2
4

2 回答 2

1
cd myfolder
for FILE in *txt ; do
    sed -i 's/test = data(x,\s*y);/test = data(z,x,y);/;/test.value(f1, "/s="[^/]*/[^/]*\(/[^/]*/[^/]*");\)$=\1=;' "$FILE"
done

if your sed isn't GNU, then use

cd myfolder
for FILE in *txt ; do
    sed 's/test = data(x, y);/test = data(z,x,y);/test.value(f1, "/s="[^/]*/[^/]*\(/[^/]*/[^/]*");\)$=\1=;' "$FILE" > "$FILE.tmp"
    mv "${FILE}.tmp" "$FILE"
done
于 2012-08-14T13:18:42.293 回答
1

将 sed 与 bash 一起使用:

for i in *.txt; do
  if [ -f $i ]; then
    sed -i 's!test = data(\s*x,\s*y)!test = data(z,x,y)!' "$i"
    sed -i 's!test.value(f1, "[^/]*/[^/]*\(/[^"]*\)");!test.value(f1, "\1");!' "$i"
  fi  
done
于 2012-08-14T13:18:06.893 回答