How to each of a list of strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to each of a list of strings
# 1  
Old 10-16-2010
How to each of a list of strings

I have a string of the form:

file1.txt file2.txt ... filen.txt

I do not know how many files there are or whether they will all be .txt files. I do, however, know that they will be separated by spaces.

My goal is to change the string so that the file names are in the appropriate directory. My desired output is:

$1/file1.txt $1/file2.txt ... $1/filen.txt

I have tried using sed and cannot figure how to make this work. I am using sh.
# 2  
Old 10-16-2010
Code:
echo "file1.txt file2.txt ... filen.txt" | sed 's/\([^ ][^ ]*\)/\$1\1/g'

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 10-16-2010
Quote:
Originally Posted by ctsgnb
Code:
echo "file1.txt file2.txt ... filen.txt" | sed 's/\([^ ][^ ]*\)/\$1\1/g'

Nice.
# 4  
Old 10-17-2010
Code:
$ echo "file1.txt file2.txt filen.txt" | ruby -e 'puts gets.split.map{|x|x="$1/"+x}.join(" ")'
$1/file1.txt $1/file2.txt $1/filen.txt

# 5  
Old 10-17-2010
Assuming, like the solutions above, that you want a literal "$1" prepended to each word (perhaps for future evaluation):
Code:
printf '$1/%s ' $str

... or if you want the actual value of $1 subsituted into the output ...
Code:
for f in $str; do
    printf '%s ' "$1/$f"
done


Last edited by alister; 10-17-2010 at 03:04 AM..
# 6  
Old 10-17-2010
I think alister's is the most elegant, though it leaves str open to wildcard expansion by the shell if you are using any.
Using sed:
Code:
sed 's|[^ ][^ ]*|$1/&|g'

similar to ctsgnb's suggestion

Last edited by Scrutinizer; 10-17-2010 at 07:40 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 10-17-2010
Good point regarding filename expansion, Scrutinizer. Thanks for mentioning it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. Shell Programming and Scripting

Common prefix of a list of strings

Is there a simple way to find the longest common prefix of a space-separated list of strings, optionally by field? For example, given input: "aaa_b_cc aaa_b_cc_ddd aaa_b_cc aaa_b_cd"with no field separator, output: aaa_b_cwith _ field separator, output: aaa_bI have an awk solution which... (1 Reply)
Discussion started by: CarloM
1 Replies

3. Programming

Pointer arithmetic for list of strings

When i want to use a list of strings char *list; i never understand it well. Here is an easy example. It took me long to figure it out, searching the web didn't help much: #include <stdio.h> int main(void) { char *list = {"foo", "bar", "baz"}; char **pl; int... (6 Replies)
Discussion started by: tornow
6 Replies

4. Shell Programming and Scripting

Take a list if strings from a file and search them in a list of files and report them

I have a file 1.txt with the below contents. -----cat 1.txt----- 1234 5678 1256 1234 1247 ------------------- I have 3 more files in a folder -----ls -lrt------- A1.txt A2.txt A3.txt ------------------- The contents of those three files are similar format with different data values... (8 Replies)
Discussion started by: realspirituals
8 Replies

5. Shell Programming and Scripting

Finding number of strings in List

I have a list of strings stored in $Lst Example set Lst = "John Fred Kate Paul" I want to return 4 in this case. (1 Reply)
Discussion started by: kristinu
1 Replies

6. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

7. Shell Programming and Scripting

How to check a list of 4-5 strings in IF condition?

Hi Unix Champs, I am a newbe to UNIX. I need to create a condition where I want to check the value of a variable with 5 different strings, if matched then exit 1. For ex: if ] then echo "yes, condition satisfied...." else echo "no, condition not satisfied..." fi ... (9 Replies)
Discussion started by: ustechie
9 Replies

8. Shell Programming and Scripting

Awk search for a element in the list of strings

Hi, how do I match a particular element in a list and replace it with blank? awk 'sub///' $FILE list="AL, AK, AZ, AR, CA, CO, CT, DE, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VT, VA, WA,... (2 Replies)
Discussion started by: grossgermany
2 Replies

9. Shell Programming and Scripting

Want unix script to search a list of strings

Want unix script to search a list of strings (stored line by line in a file), search all the matching lines of files under directories and subdirectories in different unix machines(not one box diff boxes - all the unix box names and username/password are stored with a comma delimitor in flat file)... (2 Replies)
Discussion started by: adminuser
2 Replies

10. Shell Programming and Scripting

list and export strings in a file

Hi all, I have a file looks something like this: ~~~~~~~~~~~~~~~~~~~~~~~~~ aaaa bbbbbb ccc dddddddd ~~~~~~~~~~~~~~~~~~~~~~~~~ I will like a script that can echo line1 export line1 as env variable echo line2 export line2 as env variable etc etc Thanks in advance (3 Replies)
Discussion started by: Avatar Gixxer
3 Replies
Login or Register to Ask a Question