Wrong output when writing to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wrong output when writing to file
# 1  
Old 06-27-2015
Linux Wrong output when writing to file

Hello,

I am having problem while redirecting output to a file where as on console output is proper.

Code:
for dir in */; do printf "%s, " "$dir"; ls -m "$dir"; echo; done > output.txt

Output of above command is coming in single line but when i am redirecting output to a file, single line i breaking into multiple line.

Sample Output of Console(output is coming in single line) :

Code:
activestop.nicorette/, activestop_nicorette, prod-activestop_nicorette, prod-activestop_nicorette, prod-activestop_nicorette

Sample output in file (same format is coming in file):

Code:
activestop.nicorette/, activestop_nicorette, 
activestop_nicorette,activestop_nicorette,
activestop_nicorette

please suggest.

Moderator's Comments:
Mod Comment Please use CODE tags for code segments AND for sample input and sample output.

Last edited by Don Cragun; 06-27-2015 at 05:00 PM.. Reason: Add CODE tags.
# 2  
Old 06-27-2015
You need to make it a single line list first.
Code:
list="";for l in *;do list+=", $l";done;echo "$list" > output.txt

Which is just the same as:
Code:
list=""
for l in *
do  list+=", $l"
done
echo "$list" > output.txt

hth
# 3  
Old 06-27-2015
Linux

Its is just printing main directories not the sub-directories/files..
# 4  
Old 06-27-2015
Define your desired output and give an example of input.
Also in your example code, you do quite a double work, since ls -m already produces the output you listed.

You might just change to:
Code:
ls -m "$dir"/*

Thought , you need to check if its really a directory first.

hth
# 5  
Old 06-27-2015
Not sure what you want, find might be helpful

find / will give you EVERY directory and file

find . will give you EVERY directory and file in your current directory
# 6  
Old 06-27-2015
The description of the ls -m option in the standards is:
Quote:
Stream output format; list pathnames across the page, separated by a <comma> character followed by a <space> character. Use a <newline> character as the list terminator and after the separator sequence when there is not room on a line for the next list entry. This option disables long format output.
The default line width for a regular file is probably something like 80. I have no idea what line length you have set up for your console.

You could try something like:
Code:
COLUMNS=1000000 ls -md */ */*/

to list directories in the current directory and the directories in those directories on one line (assuming that no more than one million display columns are needed to print that line). Note, however, that if the value you set for COLUMNS is larger than the value you get from:
Code:
getconf LINE_MAX

(which on many systems is 2048), then the standard text processing utilities might not work on your resulting file.

And, note that your description of what you are trying to print is very vague. Without some idea of what your file structure looks like and what you hope to print from that file structure, we are all just guessing.
# 7  
Old 06-28-2015
Hi Sea,

Below is the code i am using. On console output is fine but when i am redirecting output to file then single line is breaking in multiple lines. Can you please suggest..

Code:
[root@testserver httpd]# for dir in */; do printf "%s, " "$dir"; ls -m "$dir"; done
buckets/, apr_bucasdfsadfa_fasdfasfaool.lo, apr_buckets_simpleapr_buckets_simple.lo, apr_buckets_socketapr_buckets.lo, apr_buowekrfwqereqjncckets_reqwer.lo
build/, config_vars.sh.in, export_varsexport_varsexport_vars.pl, mkdepphf_abuse_logphf_abuse_logp.perl, rules.mksocketapr_buckets.insocketapr_buckets
include/, util_md5util_md5util.h, util_scriptutil_scriptutit.h, util_timeutil_hutil_time.h, util_xmlutil_xml.h
os/, ap_regkeyap_regkey.c, MakefileMakefile.in, MakefileMakefileMake, osabuse_logabuse_log.h
server/, export_filesexport_files, export_vars.hexport_vars.h, httpd.exphttpd.exp, libmain.lalibmain.la
support/, logresolve.pllogresolve, log_server_statuslog_server_status, phf_abuse_phf_abuse_log.cgi, split-logfilesplit-log

Code:
[root@testserver httpd]# for dir in */; do printf "%s, " "$dir"; ls -m "$dir"; done > output.txt

Code:
[root@testserver httpd]# cat output.txt
buckets/, apr_bucasdfsadfa_fasdfasfaool.lo, apr_buckets_simpleapr_buckets_simple.lo,
apr_buckets_socketapr_buckets.lo, apr_buowekrfwqereqjncckets_reqwer.lo
build/, config_vars.sh.in, export_varsexport_varsexport_vars.pl,
mkdepphf_abuse_logphf_abuse_logp.perl,
rules.mksocketapr_buckets.insocketapr_buckets
include/, util_md5util_md5util.h, util_scriptutil_scriptutit.h,
util_timeutil_hutil_time.h, util_xmlutil_xml.h
os/, ap_regkeyap_regkey.c, MakefileMakefile.in, MakefileMakefileMake,
osabuse_logabuse_log.h
server/, export_filesexport_files, export_vars.hexport_vars.h, httpd.exphttpd.exp,
libmain.lalibmain.la
support/, logresolve.pllogresolve, log_server_statuslog_server_status,
phf_abuse_phf_abuse_log.cgi, split-logfilesplit-log
[root@testserver httpd]#


Moderator's Comments:
Mod Comment Please use CODE tags for code segments AND for sample input and sample output.

Last edited by Scrutinizer; 06-28-2015 at 10:47 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting output with sed without writing to a file

HI I am trying to grep 3 characters from hostname and append a character at the end. I tried as in the following: root@abag3:~# hostname | cut -c1-3 hyu Now I am trying to append "g" at the end of this output as in the following. root@abag3:~# hostname | cut -c1-3 | sed -s... (4 Replies)
Discussion started by: Priya Amaresh
4 Replies

2. Shell Programming and Scripting

Writing output to a file in columns

Hi I am trying to write output to a file in columns I have file in the follwoing: # cat file abc def # I am trying to write next output as like # cat file abc 123 def 345 # :mad: (6 Replies)
Discussion started by: Priya Amaresh
6 Replies

3. Shell Programming and Scripting

Cat writing only one record in the output file

Hi All, I have an input file containing data as below: Input.DAT XXXXXXX|YYYYYYY|ZZZZZZZZZZ|12334446456|B|YY|111111111|111111111|111111111|111111111|15|3|NNNNNN|Y|3|AAA|111111111... (11 Replies)
Discussion started by: sagar.cumar
11 Replies

4. UNIX for Dummies Questions & Answers

Writing a script that will take the first line from each file and store it in an output file

Hi, I have 1000 files names data1.txt through data1000.txt inside a folder. I want to write a script that will take each first line from the files and write them as output into a new file. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

Writing only timing statistics output of Timer to File

I'm running long integrations on a remote server, and I'm working in terminal in a tcsh shell. I'm looking to write ONLY the timing statistics to a file. For example: $time ls >timer.out writes both the files in my current directory & the timer statistics to the file timer.out. I only... (2 Replies)
Discussion started by: elemonier
2 Replies

6. Shell Programming and Scripting

[Shell/Perl(?)] Prepending timestamps to console output & writing results to a file

I do a lot of TSM work and I embarked on what I thought would be an easy task, and I'd be very happy for any input to save the pounding my keyboard is receiving :] By default, the output of TSM's console has no timestamping, making it hard to sort through accurately. This puts my console into... (5 Replies)
Discussion started by: Vryali
5 Replies

7. Shell Programming and Scripting

Format the output from sqlplus while writing to log file.

Hi I have developed bash script to connect to database and execute .sql files. I am logging some statements in to log file using echo. While logging I am adding the date in front of the log statements which makes sense. I am unable to add date in front of output from the sqlplus and sqlldr,... (8 Replies)
Discussion started by: murtymvvs
8 Replies

8. Shell Programming and Scripting

Perl...getting wrong output?

Good morning! Im trying to write and learn at the same time a simple script. fTHe script should tell me if a number is odd or even/ #!/usr/bin/perl $num = 10; $string1 = "This number is odd"; $string2 = "This number is even"; if ($num /= 2) { print "$string1\n"; }else{ ... (3 Replies)
Discussion started by: bigben1220
3 Replies

9. Shell Programming and Scripting

writing the output of SQL into one file

Hi All, Please help me writing the below script. I have two sql queries. 1. Select count(1),Client_id from TABLE_A group by Client_id; 2. Select count(1),Client_id from TABLE_B group by Client_id; I need the output of above two sql queries in a single file. The output 2nd query should be... (4 Replies)
Discussion started by: 46019
4 Replies

10. Shell Programming and Scripting

Writing output into different files while processing file using AWK

Hi, I am trying to do the following using AWK program. 1. Read the input data file 2. Parse the record and see if it contains errors 3. If the record contains errors, then write it into Reject file, else, write into usual output file or display it on the screen Here is what I have done -... (6 Replies)
Discussion started by: vidyak
6 Replies
Login or Register to Ask a Question