TransWikia.com

Automate and looping through batch script

Stack Overflow Asked by nck_505 on December 25, 2020

I’m new to batch. I want iterate through a list and use the output content to replace a string in another file.

ls -l somefile | grep .txt | awk 'print $4}' | while read file
do
  toreplace="/Team/$file"
  sed 's/dataFile/"$toreplace"/$file/ file2 > /tmp/test.txt
done

When I run the code I get the error

 sed: 1: "s/dataFile/"$torepla ...": bad flag in substitute command: '$' 

Example of somefile with which has list of files paths

foo/name/xxx/2020-01-01.txt
foo/name/xxx/2020-01-02.txt
foo/name/xxx/2020-01-03.txt

However, my desired output is to use the list of file paths in somefile directory to replace a string in another file2 content. Something like this:

This is the directory of locations where data from /Team/foo/name/xxx/2020-01-01.txt ............

2 Answers

ls -l somefile | grep .txt | awk 'print $4}' | while read file 

No. No, no, nono.

ls -l somefile is only going to show somefile unless it's a directory.
(Don't name a directory "somefile".)
If you mean somefile.txt, please clarify in your post.

grep .txt is going to look through the lines presented for the three characters txt preceded by any character (the dot is a regex wildcard). Since you asked for a long listing of somefile it shouldn't find any, so nothing should be passed along.

awk 'print $4}' is a typo which won't compile. awk will crash.

Keep it simple. What I suspect you meant was

for file in *.txt

Then in

toreplace="/Team/$file"
sed 's/dataFile/"$toreplace"/$file/ file2 > /tmp/test.txt

it's unlear what you expect $file to be - awk's $4 from an ls -l seems unlikely.
Assuming it's the filenames from the for above, then try

 sed "s,dataFile,/Team/$file," file2 > /tmp/test.txt

Does that help? Correct me as needed. Sorry if I seem harsh.
Welcome to SO. ;)

Answered by Paul Hodges on December 25, 2020

I'm not sure if I understand your desired outcome, but hopefully this will help you to figure out your problem:

You have three files in a directory:

TEAM/foo/name/xxx/2020-01-02.txt
TEAM/foo/name/xxx/2020-01-03.txt
TEAM/foo/name/xxx/2020-01-01.txt

And you have another file called to_be_changed.txt which contains the text This is the directory of locations where data from TO_BE_REPLACED ............ and you want to grab the filenames of your three files and insert them into your to_be_changed.txt file, you can do it with:

while read file
do
  filename="$file"
  sed "s/TO_BE_REPLACED/${filename##*/}/g" to_be_changed.txt >> changed.txt
done < <(find ./TEAM/ -name "*.txt")

And you will then have made a file called changed.txt which contains:

This is the directory of locations where data from 2020-01-02.txt ............
This is the directory of locations where data from 2020-01-03.txt ............
This is the directory of locations where data from 2020-01-01.txt ............

Is this what you're trying to achieve? If you need further clarification I'm happy to edit this answer to provide more details/explanation.

Answered by jared_mamrot on December 25, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP