LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 01-02-2013, 11:10 AM   #1
Springs
Member
 
Registered: Apr 2008
Posts: 57

Rep: Reputation: 0
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?
 
Old 01-02-2013, 11:48 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
Old 01-02-2013, 12:49 PM   #3
Springs
Member
 
Registered: Apr 2008
Posts: 57

Original Poster
Rep: Reputation: 0
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
 
Old 01-02-2013, 05:13 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by Springs View Post
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 View Post
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)?
 
Old 01-03-2013, 04:41 AM   #5
Springs
Member
 
Registered: Apr 2008
Posts: 57

Original Poster
Rep: Reputation: 0
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
 
Old 01-03-2013, 04:53 AM   #6
Springs
Member
 
Registered: Apr 2008
Posts: 57

Original Poster
Rep: Reputation: 0
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
 
Old 01-03-2013, 04:57 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by Springs View Post
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?
 
Old 01-03-2013, 04:59 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by Springs View Post
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.
 
Old 01-03-2013, 06:45 AM   #9
Springs
Member
 
Registered: Apr 2008
Posts: 57

Original Poster
Rep: Reputation: 0
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.

Last edited by Springs; 01-03-2013 at 06:57 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 10 07-17-2012 10:36 AM
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 1 07-13-2012 02:03 AM
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 07:00 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 02:27 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM


All times are GMT -5. The time now is 01:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration