LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Help with script (https://www.linuxquestions.org/questions/linux-general-1/help-with-script-4175443820/)

Springs 01-02-2013 10:10 AM

Help with script
 
Hi Guys, trying to write a script which will pass a list txt file through the initial command.

What I'm trying to do is for words listed in the txt file it will then trigger a mv command to move the files from one folder into another.

below is what I currently have as a test base.

when ever I run the script it will create a "file" (not with the txt format) of the first word on the first line in the list in the destination folder and then create the files of the first word from the other lines in the directory the txt file is located in.

Code:

INPUT_FILE="/sripts/list.txt"
CONNECTIONS=`grep "." $INPUT_FILE | cut -f 1`;

for each in "$CONNECTIONS"; do

  touch /disk2/test/$each.txt;

done

the list.txt file looks like the following

"file 1"
"file 2"
"file 3"

if i run "touch /disk2/test/"file 1".txt" on the cli it works fine and creates the file with the spaces.

running the "connections" command comes through fine and shows what is expected

any ideas where i'm going wrong?

colucix 01-02-2013 10:48 AM

If you embed $CONNECTIONS in double quotes it's seen as a single value containing spaces and newlines. You have to remove the double quotes instead and use them around $each in order to take care of the space in the file names:
Code:

for each in $CONNECTIONS
do
  touch "/disk2/test/$each.txt"
done

In alternative, you can even feed a loop with the output of a command using process substitution (in bash):
Code:

while read each
do
  touch "/disk2/test/$each.txt"
done < <(awk -F"\t" '!/^$/{print $1}' $INPUT_FILE)

Hope this helps.

Springs 01-02-2013 11:49 AM

Thanks for that.

The first one seem'd to create individual files for every separate word in the txt file.

The 2nd method creates all the files as expected but there is an extra space between the last letter and the .txt
Code:

Prime .txt

colucix 01-02-2013 04:13 PM

Quote:

Originally Posted by Springs (Post 4861401)
The first one seem'd to create individual files for every separate word in the txt file.

Yes, sorry. Indeed the fields in the list are separated by newline, whereas the shell uses blank spaces, newlines and tabs according to the value of the IFS variable. You have to change IFS to newline only and reset it later if necessary. Example:
Code:

OLD_IFS="$IFS"

IFS='
'

for each in $CONNECTIONS
do
  touch "/disk2/test/$each.txt"
done

IFS="$OLD_IFS"

Quote:

Originally Posted by Springs (Post 4861401)
The 2nd method creates all the files as expected but there is an extra space between the last letter and the .txt

The extra space depends on the actual content of list.txt. Please, can you post a relevant part of it (using CODE tags to preserve spaces and tabs)?

Springs 01-03-2013 03:41 AM

had another look this morning.

as i was just using "Watch ls" to check the files were creating correctly it showed as having a space. looking at the actual files where the space would be there is a "?"

i've checked the contents of the list txt file and can't see any spaces at the end of the text.

this is how they are listed in the txt file.
Code:

Theory
Castle
Prime
Skies


Springs 01-03-2013 03:53 AM

ok remade the txt file within the terminal instead of creating it within windows.

It now works and gives the correct response with no spaces or random characters after the initial words or .txt extension

colucix 01-03-2013 03:57 AM

Quote:

Originally Posted by Springs (Post 4861883)
this is how they are listed in the txt file.
Code:

Theory
Castle
Prime
Skies


I don't understand at this point why you used
Code:

grep "." $INPUT_FILE | cut -f 1
to extract the file names. The grep part assumes there are empty lines you want to skip (otherwise I don't see the reason to match any single character). The cut part extract the first TAB separated field. Maybe what you have shown is not part of the REAL list.txt file?!? And what about files with space in their names, since the problem originated from them?

colucix 01-03-2013 03:59 AM

Quote:

Originally Posted by Springs (Post 4861889)
ok remade the txt file within the terminal instead of creating it within windows.

It now works and gives the correct response with no spaces or random characters after the initial words or .txt extension

Good to know. I still have doubts about the original input file, but I'm glad you solved your issue.

Springs 01-03-2013 05:45 AM

some of the commands i started with was from another script that was given to me by an old colleague which partially did the same thing but was to run individual commands which was located in the txt file and was separated by a "," so was listed as name,command hence the grep command etc.

Also i think when i created the original list within windows linux didn't like some format is must of left.

Managed to get it all working how i want it.

Shouldn't have any problems now, so big thanks for the help in this.


All times are GMT -5. The time now is 05:33 PM.