Formatting the o/p of ls command to a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Formatting the o/p of ls command to a file
# 1  
Old 04-06-2010
Formatting the o/p of ls command to a file

HI,

I require only the file name and the user information from the ls output into a file. The layout of the file is already defined as:
User id is in the first 7 bytes
FName is in the next 50 bytes

Assuming that user id is less than or = 7 bytes and
File name is less than or = 50 bytes

Can the above be achieved?

Thanks
Meghna
# 2  
Old 04-06-2010
Code:
ls -al | awk {'print $3,$9'} | sed '1,3d' > file

Sed was included to remove the first two lines ("." and ".."). If you want "." and ".." to be included, remove "sed '1,3d'".
# 3  
Old 04-06-2010
Thanks sun2ecliptic.

But:
ls -al | awk {'print $3,$9'} | sed '1,3d' > file
will output the user & file name information into the file. How can I format this information such that the user id is in the first 7 bytes and file name is in the next 50 bytes. Can I do this simultaneously in the awk command or do I have to manually do this and create a file 2.

Also, I am sorry but I am not clear what you mean by:
first two lines ("." and "..").


Thanks
# 4  
Old 04-06-2010
embedded pipes to highlight field terminations:

Code:
 ls -l |while read a b c d e f g h i ;do printf "%7.7s|%50.50s|\n" $c $i ;done

you could also force left justified:

Code:
ls -l |while read a b c d e f g h i ;do printf "%-7.7s|%-50.50s|\n" $c $i ;done


Last edited by curleb; 04-06-2010 at 03:28 PM.. Reason: purely justifiable...
# 5  
Old 04-06-2010
Code:
ls -al | awk '{print substr($3,0,7) " " substr($9,0,50)}' | sed '1,3d'

In this example, awk uses the substr function to print the first 7 characters of the username field, include whitespace as a field separator, and the first 50 characters of the directory/filename field.

"." and ".." represent the current working directory and the parent directory. They do not represent any files within the directory you are looking for, just the current and parent directories themselves (of the listing).

Last edited by sun2ecliptic; 04-06-2010 at 03:37 PM.. Reason: ..addin' more information.. arrrrg!
# 6  
Old 04-15-2010
Thanks a lot sun2ecliptic and curleb
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting Read Command

Hi All, I am using HP Unix and trying to write a script in menu style. At the end of list, I will accept the input from keyboard and take action accordingly. Uptill here it is ok. However, I want to hide the cursor, display it as blinking line after the prompt. How do I achive it. I am using... (2 Replies)
Discussion started by: angshuman
2 Replies

2. Shell Programming and Scripting

Wall command with nice text formatting

with using wall command, how can i have a carriage return in my broadcast message. i try to broadcast from a file, i were to use "cat myfile | wall" for broadcasting. but when the message broadcast somehow the format run away. this the text in my file: line 1 line 2 line 3 when broadcast ... (3 Replies)
Discussion started by: lsy
3 Replies

3. Shell Programming and Scripting

Formatting for mailx command

Hello all, I am trying to send an email using mailx command. I have a txt file which has formatting like: root@machine# cat /export/home/test/summary.txt T A P O U T - R A T I N G - A U D I T - S U M M A R Y - R E P O... (2 Replies)
Discussion started by: Junaid Subhani
2 Replies

4. UNIX for Advanced & Expert Users

Paste command formatting

Hi, I was trying to concatenate some files using paste command along with some formatting but getting stuck. The problem is: cat 1.txt A cat 2.txt B C cat3.txt D E cat 4.txt G H (5 Replies)
Discussion started by: abhi1988sri
5 Replies

5. Shell Programming and Scripting

Need help regarding formatting(comm -23 command)

Hello all , I have two files a.txt and b.txt which have same content . They contain data that is fetched from database through a java program. When I delete a line in a.txt and run the below command comm -13 a.txt b.txt I am not getting the expected result i.e. the line i deleted from... (5 Replies)
Discussion started by: RaviTej
5 Replies

6. Shell Programming and Scripting

Formatting the output of ps -eo command

Hi ive been tasked to create a warning email which will be sent when a process exceeds a given percentage of CPU, Ive created the script where it runs: ps -eo pri,pid,user,nice,pcpu,comm | awk '{if($5 >= 2)print $0}' >> /export/home/tjmoore/file2 2>/dev/null I would then run a mail program... (21 Replies)
Discussion started by: 02JayJay02
21 Replies

7. Shell Programming and Scripting

Formatting list of IP addresses into a grep command

Hi I have an input file with a list of random IP addresses, each on a new line. Below is just an example as I omitted the real IP addresses for obvious reasons. Input: random_ip.txt 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1... (7 Replies)
Discussion started by: lewk
7 Replies

8. UNIX for Advanced & Expert Users

Formatting Substitution Command Not Working in vi

Unix Gurus, I have a text file I have FTP'd to UNIX so that I can use it to load into our Baan system. When vi the file, I can see that there are formatting characters at the end of each line (^M). To get rid of these, I have read that the following should work: :%s/^M$//g - with the ^M... (11 Replies)
Discussion started by: ERPKEN
11 Replies

9. AIX

Trouble formatting egrep command with AWK

Hi, I'm new to scripting and AIX. I'm running the following: lspv | awk '{ print "lspv",$1" | egrep 'PP\|PHYSICAL'; lspv -l",$1 }' Which creates this command: lspv hdisk0 | egrep PP|PHYSICAL; lspv -l hdisk0 lspv hdisk1 | egrep PP|PHYSICAL; lspv -l hdisk1 Troube is, I need the... (2 Replies)
Discussion started by: cruiser
2 Replies

10. Shell Programming and Scripting

is running this command via ssh possible? (formatting issues)

Here is the command I want to run: for pkg in `pkginfo | grep -i VRTS | awk '{print $2}'`; do showrev -p | grep $pkg; done | awk '{print $2 "\t" $7}' | uniq It returns the package info in a form such as: 113210-03 VRTSfspro 112392-06 VRTSvmman 113596-03 VRTSvmpro... (1 Reply)
Discussion started by: LordJezo
1 Replies
Login or Register to Ask a Question