spaces in array field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting spaces in array field
# 1  
Old 08-19-2010
spaces in array field

I have a file, names(i) where each entry is 'first last' name. 'cat names' is fine. But in a shell script

Code:
>for file in $(cat names)
> do
>   echo $file
> done

the first and last name appear on 2 lines. I have tried escaping and quoting the space but to no avail. The names are to be used in renaming jpg files and will be used to print labels with picture and name. What must I do?

Chuck

Last edited by Scott; 08-19-2010 at 07:18 PM.. Reason: Code tags
# 2  
Old 08-19-2010
Code:
while read file
do
echo $file
done < names

# 3  
Old 08-19-2010
Code:
>for file in "$(cat names)"
> do
> echo $file
> done

# 4  
Old 08-19-2010
Quote:
Originally Posted by ygemici
Code:
>for file in "$(cat names)"
> do
> echo $file
> done

Hi.

All that does is give the entire file to "$file" in one go... and it's also eligible for the UUOC award.

The while-loop approach is the better one.
# 5  
Old 08-20-2010
Quote:
Originally Posted by chuckmg
I have a file, names(i) where each entry is 'first last' name. 'cat names' is fine. But in a shell script

Code:
>for file in $(cat names)
> do
>   echo $file
> done

the first and last name appear on 2 lines. I have tried escaping and quoting the space but to no avail. The names are to be used in renaming jpg files and will be used to print labels with picture and name. What must I do?

Chuck
Please post your input file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove spaces from between words that are in a field

Hi all, Is there a sed/awk cmd that will remove blank space from between words in a particular field, replacing with a single space? Field containing 'E's in the example below: Example input file: AAAAA AA|BBBB|CCCCCCC|DDDDDD |EEEE EEEEEE| FFF FFFFF| ... (6 Replies)
Discussion started by: dendright
6 Replies

2. UNIX for Dummies Questions & Answers

AWK print last field including SPACES

I have simple test.sh script, see below: bill_code=`echo $record | awk -F"|" '{print $1}'` Fullname=`echo $record | awk -F"|" '{print $3}'` email=`echo $record | awk -F\ '{print $4}'` The last field contains spaces: see csv below: A0222|Y|DELACRUZ|-cc dell@yahoo.com-cc support@yahoo.com ... (9 Replies)
Discussion started by: quay
9 Replies

3. Shell Programming and Scripting

Loop through array of arrays of string with spaces

Hi I'm trying to loop through an array that contains other arrays and these arrays consist of strings with spaces. The problem is that I can't seem to preserve the spacing in the string. The string with spaces are either divided into multiple items if I change IFS to \n or all the elements of... (4 Replies)
Discussion started by: kidmanos
4 Replies

4. UNIX for Dummies Questions & Answers

Removing spaces in the second field alone

Consider my input string as "abc|b f g|bj gy" I am expecting the output as "abc|bfg|bj gy". Please let me know how to achieve this in unix? Thanks (8 Replies)
Discussion started by: pandeesh
8 Replies

5. Shell Programming and Scripting

Help with removal of blank spaces from the second field!

Hi everyone.. I'm trying to eliminate multiple whitespaces from a file.. I must make use of shell script to eliminate whitespaces.. Take a look at the sample file 1 int main() 2 { 3 int a,b; 4 printf("Enter the values of a and b"); 5 scanf("%d%d",&a,&b); 6 if(a>b) ... (6 Replies)
Discussion started by: abk07
6 Replies

6. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

7. Solaris

removing special characters, white spaces from a field in a file

what my code is doing, it is executing a sql file and the resullset of the query is getting stored in the text file in a fixed format. for that fixed format i have used the following code:: Code: awk -F":"... (2 Replies)
Discussion started by: priyanka3006
2 Replies

8. Shell Programming and Scripting

array in ksh with elems containing spaces

Hi all, I've a .csv file containing data per line delimited with '|' (The fields may contains elements with spaces). e.g. (really a sample) ID|Name|fon 12345|Celal Dikici|+4921123456 12346|Celal Dikici Jun.|+4921123456 12347|Celal Dikici Sen.|+4921123456 12348|Celal|+4921123456... (3 Replies)
Discussion started by: Celald
3 Replies

9. UNIX for Dummies Questions & Answers

problem with the blank spaces in array

Hi all! i need your help. I'm getting started with this... in need to insert in an array string values. But the thing is that this strings have blank spaces... for example if a want to put "filed1 = " and "field2 = " .... in the array , when i want to print all the fields, it only shows... (4 Replies)
Discussion started by: kamicasse
4 Replies

10. Shell Programming and Scripting

How to get array to not split at spaces?

I have been working on some code for a while, that will parse a log file, look for a specified time discrepancy between entries, and then print that line +/- n other lines out to a file... #!/bin/bash file=$1 # The input log file maxTime=$2 # The time discrepancy to look for n=$3 ... (1 Reply)
Discussion started by: jjinno
1 Replies
Login or Register to Ask a Question