differentiate between spaces and new-lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting differentiate between spaces and new-lines
# 1  
Old 07-01-2011
differentiate between spaces and new-lines

Hi -

Here's the problem I'm encountering......My script logs in to remote FTP through password-less "sftp" and must get the list of sub-directories under a given path. I know many of the unix commands don't work in "sftp" login so I cannot know which one is a "directory" and which one is a "file". So I thought of copying the output of ls -l command in a file at local unix server and then going thru it to decide which ones are directories.

Having told my end of the story here's what I'm doing?
Code:
 
echo "ls -l $remote_ftp_path" > $batch_file
list_of_sub-dir=`sftp -b $batch_file $USER_ID@$IP_ADDRESS`

What happens?
Value of "list_of_sub-dir" variable has all the info about sub-dir, permissions, size, etc in one single line (as expected):
Code:
sftp> ls -l /remote/ftp/path drwxr-xr-x 2 user group 4096 Jun 13 15:55 remote_dir_1 -rw-r--r-- 1 user group 5090607 Jun 21 08:38 remote_file_1 -rw-r--r-- 1 user group 6917238 Jun 21 08:37 remote_file_2

and so on....

which I want in below format (after stripping "sftp> ls -l /remote/ftp/path")
Code:
 
drwxr-xr-x 2 user group 4096 Jun 13 15:55 remote_dir_1
-rw-r--r-- 1 user group 5090607 Jun 21 08:38 remote_file_1
-rw-r--r-- 1 user group 6917238 Jun 21 08:37 remote_file_2

Then I had a crazy idea of translating (only) spaces (and not new-lines) into commas
Code:
echo $list_of_sub-dir | tr " " ","

here's what I get instead
Code:
sftp>,ls,-l,/remote/ftp/path,drwxr-xr-x,2,user,group,4096,Jun,13,15:55,remote_dir_1,-rw-r--r--,1,user,group,5090607,Jun,21,08:38,remote_file_1,-rw-r--r--,1,user,group,6917238,Jun,21,08:37,remote_file_2

Can anyone please help me to get this? I really don't know what should I do to somehow preserve "\n" (new-lines) in the variable and not confuse them with "spaces"?!

-dips
# 2  
Old 07-01-2011
From what you've posted, it's just a matter of correct quoting:
Code:
$ list_of_subdir="This
> is
> a
> Test"
$ echo $list_of_subdir
This is a Test
$ echo "$list_of_subdir"
This
is
a
Test
$

The background is this: without the quotes, every token separated by one or more whitespaces is given as an argument to echo, which then outputs them separated by exactly 1 space. With the quotes, however, the whole variable is passed as a single argument, including the newlines.
These 2 Users Gave Thanks to pludi For This Post:
# 3  
Old 07-01-2011
Thanks a ton!! Pludi.

I was being so silly!! Smilie

-dips
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing certain positions in lines with spaces

Hello, I have a file with hundreds of lines. Now I need to replace positions 750-766 in each line (whatever there is there) with spaces... how can I do that? Which command to use? The result will be all the lines in the file will have spaces in positions 750-766. Thanks! (3 Replies)
Discussion started by: netrom
3 Replies

2. Shell Programming and Scripting

Spaces between the lines

How to get spaces between the two lines I have file File1 A.txt B.txt File2 C.txt D.txt Doing cat and appending to one file i.e file3 when i was doing append i need output like below File3 File1 (3 Replies)
Discussion started by: satish1222
3 Replies

3. Shell Programming and Scripting

Reform Lines in File without blank lines and spaces

Hello All, I have a file with data as below. Each line consists of 21 fields. I am not able to load them back to the database. 50733339,"834","834 ","005010X279A1","N","Y","007977163","0001 ",30,"2110D ","EB ","EB007 ","2 ","Conditional Required Data Element Miss ing... (3 Replies)
Discussion started by: Praveenkulkarni
3 Replies

4. Shell Programming and Scripting

remove spaces and lines that start with --

Is it possible to remove empty lines between >humid-sets (bold) and also humidset that start with -- (for ex: > humid3 | () : | (+) ) Thanx in advance Note: The humid sets will be in thousands and lines will be more than 100 thousand. input > humid1 | () : | (+)... (7 Replies)
Discussion started by: quincyjones
7 Replies

5. Shell Programming and Scripting

Having a for loop read in lines with spaces?

Is this possible? I have a for loop in a shell script reading a list, but I want each line to be a loop, not each thing with a space. Here is the example: HOSTLIST="\ 1.2.3.4 serverA 1.2.3.5 serverB" for NBUHOST in `echo $HOSTLIST` do ssh ${SERVERNAME} "echo "${NBUHOST}"... (3 Replies)
Discussion started by: LordJezoX
3 Replies

6. UNIX for Dummies Questions & Answers

using tr to print spaces between lines

hi forum i was wondering can you use tr to print out a file like this i see that there is no way you could do that i feel i see that there is no way you could do that i feel i see that there is no way you could do that i feel i see that there is no way you could do that i feel i see that... (2 Replies)
Discussion started by: ShinTec
2 Replies

7. Shell Programming and Scripting

Deleting lines that contain spaces in a txt file

I need some help deleting lines in a file that contain spaces. Im sure awk or sed will work but i dont know much about those commands. Any help is appreciated :D (7 Replies)
Discussion started by: r04dw4rri0r
7 Replies

8. Shell Programming and Scripting

Spaces in Lines for Variables

All, I am driving myself crazy over this one. I have run a find command against a volume on a nas. That returns a full listing of path and file name. This is an example of one line of output. I redirected the output of the find command to a file. ... (4 Replies)
Discussion started by: bubbwe
4 Replies

9. Shell Programming and Scripting

How to remove plank spaces at the end of lines

Hello friends, I want to remove blank spaces at the end of lines. I use sed command to do this but it is not working correctly. sed ‘s/ $//’ file_name Can some body tell me what is the proper way to remove blank spaces at the end of a limes. Thanks, Mahesh Fernando. (3 Replies)
Discussion started by: maheshsri
3 Replies

10. UNIX for Dummies Questions & Answers

delete blank lines or lines with spaces only

hi there i'm trying to delete blank lines and or lines with spaces only from a series of files in an directory. to do so, i'm using this: for files in `ls /users/myname/pesop* 2>/dev/null` do grep -v ^$ $files > newfile mv newfile $files done now, this works great for blank lines but... (3 Replies)
Discussion started by: vascobrito
3 Replies
Login or Register to Ask a Question