wrinting "ls" results on 1 line....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting wrinting "ls" results on 1 line....
# 1  
Old 11-09-2004
wrinting "ls" results on 1 line....

...Hi ,

i would run "ls" command through 1 directory like this :

#ls /dir1/dir2/file* > fic.txt

and having the result on 1 line separated by a blank.

Then with this result , i could run a script like this :

for i in fic.txt
do.....

it seems to be simple but the "for" statement needs to have each entry as a field...

I'm sure you have an idea .......

thanks in advance

Christian
# 2  
Old 11-09-2004
The trouble with that approach would be if the filename had some form of whitespace in it, the filename would be split.

I'd rather just do an
ls > foo.txt

Then, use a while loop
Code:
while read line
  # do something to "$line"
  echo "$line"
done < foo.txt

To answer your original question, you could do something like
ls -m | tr -d ',' > foo.txt to get a space seperated list of files and directories on one line, but I wouldn't recommend it.

Cheers
ZB
# 3  
Old 11-09-2004
Thanks for the double answer , the "while" statement is the solution !

thanks again
Christian
# 4  
Old 11-09-2004
ls -m | tr "," " " | tr "\n" " " | tr -s " "



does the job toooo ....
# 5  
Old 11-09-2004
Indeed, if the output is greater than the number of columns specified in the COLUMNS environment variable, then you will need to replace newlines.

I still wouldn't use this method though as filenames with whitespace will still make the code fall down.

Cheers
ZB
# 6  
Old 11-09-2004
while read line
do
echo "$line\c " ; echo " \c"
done < foo.txt



\c to echo prevents echo to take a new line ;
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search file containing ps results for a match "my.cnf" and then for a second match . "ok:" and

I need to find two matches in the output from ps. I am searching with ps -ef |grep mysql for: my.cnf /bin/sh /usr/bin/mysqld_safe --defaults-file=/data/mysql/master/agis_core/etc/my.cnf after this match I want to search back and match the hostname which is x number of lines back, above the... (2 Replies)
Discussion started by: bash_in_my_head
2 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

4. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

5. Shell Programming and Scripting

Move a line containg "char" above line containing "xchar"

Okay, so I have a rather large text file and will have to process many more and this will save me hours of work. I'm not very good at scripting, so bear with me please. Working on Linux RHEL I've been able to filter and edit and clean up using sed, but I have a problem with moving lines. ... (9 Replies)
Discussion started by: rex007can
9 Replies

6. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

7. UNIX for Dummies Questions & Answers

"echo *.out" - can I separate the results by a new line?

Greetings, Let's say I want to find files matching "*.out" in the current dir. I can do a echo *.out. However, the result is separated by a white space. Is there a way to have the results separated by a new line instead? I know this can be done by using find -maxdepth 1 -name '*.out'. The... (5 Replies)
Discussion started by: rasp
5 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question