Read space in script


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Read space in script
# 1  
Old 10-27-2015
Read space in script

Hi All,

Please let me know how to read space between 2 words in script. It is reading only first chars ES,NZ. But after space it is not reading next 3 chars.

Code:
 ES LPI 
 NZ GBL 
 FR LPI 
 DK LPI
  
 for v_sob_cntry_cd in `cat /shared/set_of_books_cntry_cd_list.lst`
 do 
 Fname=`echo "GEOR_"$v_sob_cntry_cd"_T_AND_L.dat"`
 RC=$?

Thanks,
# 2  
Old 10-27-2015
You have not told us which OS, or shell so......
Manually longhand, using OSX 10.7.5, default bash terminal...
Code:
Last login: Tue Oct 27 09:06:03 on console
AMIGA:barrywalker~> echo ' ES LPI 
>  NZ GBL 
>  FR LPI 
>  DK LPI' > /tmp/file
AMIGA:barrywalker~> cat /tmp/file
 ES LPI 
 NZ GBL 
 FR LPI 
 DK LPI
AMIGA:barrywalker~> while read line
> do
> line=( $line )
> echo "${line[0]} , ${line[1]} ."
> done < /tmp/file
ES , LPI .
NZ , GBL .
FR , LPI .
DK , LPI .
AMIGA:barrywalker~> _

# 3  
Old 10-27-2015
I'm somewhat unsure what you mean by "read space between 2 words". Your code snippet doesn't read anything but produces a list of values that the for loop variable then assumes. Completing your code and echoing $Fname in the loop, I get
Code:
GEOR_ES_T_AND_L.dat
GEOR_LPI_T_AND_L.dat
GEOR_NZ_T_AND_L.dat
GEOR_GBL_T_AND_L.dat
GEOR_FR_T_AND_L.dat
GEOR_LPI_T_AND_L.dat
GEOR_DK_T_AND_L.dat
GEOR_LPI_T_AND_L.dat

Isn't that what you'd expect?

Guessing that you are using some Bourne shell descendant, IFS - which is used to split an input line into words - is set to "<space><TAB><newline>" by default. So it wouldn't occur neither in a for loop value list nor in a while read loop.
# 4  
Old 10-27-2015
I am looking the below output. Also I am using the below unix version
Code:
bash-4.1$ uname -a
Linux ad2222aa 2.6.32-504.8.1.el6.x86_64 #1 SMP Fri Dec 19 12:09:25 EST 2014 x86_64 x86_64 x86_64 GNU/Linux

 GEOR_ES_LPI_T_AND_L.dat
GEOR_NZ_GBL_T_AND_L.dat
GEOR_FR_LPI_T_AND_L.dat
GEOR_DK_LPI_T_AND_L.dat

# 5  
Old 10-27-2015
Quote:
Originally Posted by kiranparsha
I am looking the below output. Also I am using the below unix version
Code:
bash-4.1$ uname -a
Linux ad2222aa 2.6.32-504.8.1.el6.x86_64 #1 SMP Fri Dec 19 12:09:25 EST 2014 x86_64 x86_64 x86_64 GNU/Linux

 GEOR_ES_LPI_T_AND_L.dat
GEOR_NZ_GBL_T_AND_L.dat
GEOR_FR_LPI_T_AND_L.dat
GEOR_DK_LPI_T_AND_L.dat

Hello kiranparsha,

Considering your Input_file is as follows.
Code:
 cat Input_file
 ES LPI
 NZ GBL
 FR LPI
 DK LPI

Then following code may help you to achieve as per your posted output.
Code:
awk '{print "GEOR" OFS $1 OFS $2 OFS "T_AND_L.dat"}' OFS="_"  Input_file

Output will be as follows.
Code:
GEOR_ES_LPI_T_AND_L.dat
GEOR_NZ_GBL_T_AND_L.dat
GEOR_FR_LPI_T_AND_L.dat
GEOR_DK_LPI_T_AND_L.dat

Thanks,
R. Singh
# 6  
Old 10-27-2015
How about
Code:
while read line; do echo "GEOR${line// /_}T_AND_L.dat"; done < file
GEOR_ES_LPI_T_AND_L.dat
GEOR_NZ_GBL_T_AND_L.dat
GEOR_FR_LPI_T_AND_L.dat
GEOR_DK_LPIT_AND_L.dat

This strongly depends on the spaces as given in post#1!
# 7  
Old 10-27-2015
Thanks Rudic . Its working fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Read Only Permission after the space is full.

Hi, Is there any chance that a file system that mounted on the server becomes read only when the space in that file system becomes full? Regards, Sreejith (9 Replies)
Discussion started by: Sreejith K
9 Replies

2. Shell Programming and Scripting

Getting foreach to read a parameter with blank space

my program is designed to take the first parameters as extension, then the rest of the parameters as files to be searched for and, if found, modified by the extension. If not found, it prints an error. Everything is great until: ./chExt.sh 'com' 'king cobra.dat' where $file splits up the two... (2 Replies)
Discussion started by: username652719
2 Replies

3. Shell Programming and Scripting

space after read prompt?

Hello, Unix-Forums. How can I make a Space after a read prompt? let's assume: read -p "Are you good?:" varthe output would be ( | is the cursor ): Are you good?:|But I want it to be: Are you good?: |That's what I mean. How would I do that? (2 Replies)
Discussion started by: intelinside
2 Replies

4. Shell Programming and Scripting

Unable to read the first space of a record in while loop

I have a loop like while read i do echo "$i" . . . done < tms.txt The tms.txt contians data like 2008-02-03 00:00:00 <space>00:00:00 . . . 2010-02-03 10:54:32 (2 Replies)
Discussion started by: machomaddy
2 Replies

5. Shell Programming and Scripting

How to read the filenames with space character in it

Hi, My Requirement is to read the filenames which possess space charatcer in it and pass the same file name to the other shell script as input parameter. Please provide your valuable suggestion. (5 Replies)
Discussion started by: cnraja
5 Replies

6. Homework & Coursework Questions

Read text, handle white space

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The problem would be to make the program tolerate the white space and still read the string of characters. the... (1 Reply)
Discussion started by: mgyeah
1 Replies

7. Solaris

How to read EMC disk space

Hi I am using Solaris8.I want to find the total disk space of my server.Can anyone please tell that which field in below mentioned code signifies the disk space and whether this space is in Mb or GB. c11t0d52 <EMC-SYMMETRIX-5264 cyl 4 alt 2 hd 15 sec 64> ... (4 Replies)
Discussion started by: sharmaankur85
4 Replies

8. UNIX for Advanced & Expert Users

how can I read the space in the end of line

cat file1|while read i do echo "$i"|wc done with this command the space in the end of the line not considered how can solve that for example: read h "hgyr " echo "$h"|wc 4 (2 Replies)
Discussion started by: Ehab
2 Replies

9. Shell Programming and Scripting

How to read filenames with space in between

Hi All, I need to read filenames with space in between in a for loop like Integration of utility projects Integration of hdf projects I copied these files from a windows system and as you know windows filename has spaces in between them. But while unix is reading the filename in a for... (12 Replies)
Discussion started by: dayanandra
12 Replies

10. Shell Programming and Scripting

How to keep white space is being deleted using read

I am using Posix shell to write a script. The problem I am having is that when I use the read command to go through a file I lose the tabs. How can I keep this from happening? (1 Reply)
Discussion started by: keelba
1 Replies
Login or Register to Ask a Question