shell script to find noof characters in a file name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers shell script to find noof characters in a file name
# 1  
Old 03-08-2010
shell script to find noof characters in a file name

hiiii

shell script to find noof characters in a file name, when you run ls -l (using awk)

I tried with this

Code:
ls -l > temp
awk -F"," '{print $1 "   " expr length $9}' temp

but it give some other value instead of file name length (error value like , 563,54,55,56....).How to prnint the the file name length ..?

Code:
bash-3.1$ sh 21.sh
total 24   8
-rw-r--r-- 1 krishna users 112 2010-03-08 06:05 21.sh   53
-rw-r--r-- 1 krishna users  47 2010-03-08 05:42 21.sh~   54
-rw-r--r-- 1 krishna users  34 2010-03-08 06:19 new1.sh   55
-rw-r--r-- 1 krishna users   2 2010-03-08 06:15 new1.sh~   56
-rw-r--r-- 1 krishna users   0 2010-03-09 08:06 temp   52
-rw-r--r-- 1 krishna users  69 2010-03-08 06:08 test1.sh   56
-rw-r--r-- 1 krishna users   2 2010-03-08 05:55 test1.sh~   57
bash-3.1$


Last edited by pludi; 03-09-2010 at 02:21 AM.. Reason: code tags, please...
# 2  
Old 03-09-2010
Bug

Use the following script for getting the filename characters count.

Code:
ls -l | tail -n+2 > input
while read line
do
no=`echo $line | wc -c `
echo $line $no
done < input

# 3  
Old 03-09-2010
You can do it also with awk...

Code:
ls -l | awk '{print $1,length($9)}'

# 4  
Old 03-09-2010
I'm stii getting the same old wrong output............does not get the length of file name
# 5  
Old 03-09-2010
Quote:
Originally Posted by krishnampkkm
I'm stii getting the same old wrong output............does not get the length of file name
what do you get when you run...

Code:
ls -l

# 6  
Old 03-09-2010
hello, is this what you looking for?
Code:
ls -l | nawk '{print $NF,length($NF)}'

# 7  
Old 03-10-2010
The actual result I need is

ls -l
total 36
-rw-r--r-- 1 krishna users 146 2010-03-10 10:11 21.sh
-rw-r--r-- 1 krishna users 147 2010-03-10 10:11 21.sh~
-rw-r--r-- 1 krishna users 505 2010-03-09 11:42 input
-rw-r--r-- 1 krishna users 34 2010-03-08 06:19 new1.sh
-rw-r--r-- 1 krishna users 2 2010-03-08 06:15 new1.sh~
-rw-r--r-- 1 krishna users 568 2010-03-10 10:11 temp
-rw-r--r-- 1 krishna users 69 2010-03-08 06:08 test1.sh
-rw-r--r-- 1 krishna users 2 2010-03-08 05:55 test1.sh~
-rw-r--r-- 1 krishna users 98 2010-03-09 11:42 today.sh
-rw-r--r-- 1 krishna users 0 2010-03-09 11:42 today.sh~



But I'm getting like

bash-3.1$ sh 1.sh
2
5
6
-rw-r--r-- 1 krishna users 505 2010-03-09 11:42 input 5
-rw-r--r-- 1 krishna users 34 2010-03-08 06:19 new1.sh 7
-rw-r--r-- 1 krishna users 2 2010-03-08 06:15 new1.sh~ 8
-rw-r--r-- 1 krishna users 0 2010-03-10 10:12 temp 4
-rw-r--r-- 1 krishna users 69 2010-03-08 06:08 test1.sh 8
-rw-r--r-- 1 krishna users 2 2010-03-08 05:55 test1.sh~ 9
-rw-r--r-- 1 krishna users 98 2010-03-09 11:42 today.sh 8
-rw-r--r-- 1 krishna users 0 2010-03-09 11:42 today.sh~ 9


by executing lines of my script....

ls -l > temp
awk '{print $ $NF,length($NF)}' temp
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Shell script to find file type

1. The problem statement, all variables and given/known data: Write a shell script that takes a single command line parameter, a file path (might be relative or absolute). The script should examine that file and print a single line consisting of the phrase: Windows ASCII if the files is an... (4 Replies)
Discussion started by: kwatt019
4 Replies

2. UNIX for Dummies Questions & Answers

Find error in the shell script file

echo "******Select Option:******" echo "1 - script1" echo "2 - script2" echo "3 - script3 " read option echo "You have selected" $option"." if then /scratch/username/script1.sh elif then /scratch/username/script2.sh elif then /scratch/username/script3.sh else echo "Please try again... (12 Replies)
Discussion started by: Dish
12 Replies

3. Shell Programming and Scripting

share a shell script which can replace weird characters in directory or file name

I just finish the shell script . This shell can replace weird characters (such as #$%^@!'"...) in file or directory name by "_" I spent long time on replacing apostrophe in file/directory name added: 2012-03-14 the 124th line (/usr/bin/perl -i -e "s#\'#\\'#g" /tmp/rpdir_level$i.tmp) is... (5 Replies)
Discussion started by: begonia
5 Replies

4. UNIX for Dummies Questions & Answers

Shell script find word from one file and insert in another file

Hi, I am new to shell scripting. I need a bash shell scripts which search and grep a parameter value from input.txt file and insert it in between two semicolon of second line of output.txt file. For example The shell script search an IP address as parameter value from input.txt ... (2 Replies)
Discussion started by: sunilkumarsinha
2 Replies

5. Shell Programming and Scripting

Shell Script Find in File

Right, noob to shell scripting, playing a round for practice, wrote the following but it doesn't seem to work as expected, how could I fix/improve this script? #!/bin/bash #set -v #set -x case $# in 1) echo Searching for $1 in '*'; find . -iname '*' 2>/dev/null | xargs grep "$1" -sl... (3 Replies)
Discussion started by: Pezmc
3 Replies

6. Shell Programming and Scripting

How to find yesterdays file - shell script

Hey guys - i have a script (below) that searches for current files in a particular directory. However i was wondering how to make it search for "yesterdays" file. For instance it looks for a file from yesterday and no older than that. I used stat command to check for file information: ... (6 Replies)
Discussion started by: DallasT
6 Replies

7. Shell Programming and Scripting

writing shell script to find line of invalid characters

Hi, I have to write s script to check an input file for invalid characters. In this script I have to find the exact line of the invalid character. If the input file contain 2 invalid character sat line 10 and 17, the script will show the value 10 and 17. Any help is appreciated. (3 Replies)
Discussion started by: beginner82
3 Replies

8. Shell Programming and Scripting

Bold characters in a file using Shell script

Hi, When I am running below mentioned script then the characters become bold but after opening the same file in Windows, Instead of getting bold characters i am getting some garbage value for \033Kunal Dixit Output in Windows (after ftp the file): but in windows , i am getting My name is... (0 Replies)
Discussion started by: kunal_dixit
0 Replies

9. Shell Programming and Scripting

help removing characters for output file in shell script

hi i'm new to shell scripts and have a small problem i am running a batch converter that returns all flash .flv files in a directory and create a png image from each one the problem i have is the $1 variable , its ok on the first call but on the secound call $1.png , i have extra... (1 Reply)
Discussion started by: wingchun22
1 Replies

10. Shell Programming and Scripting

Shell script to Find file size

Hi, I am writing a script which takes the input file name and concat as a new file by appending a "1" to the file name. However i am not able to get the size of this new file. I am not sure where i am going wrong. Please check the script and help me get this working. #!/bin/sh ... (1 Reply)
Discussion started by: ragsnovel
1 Replies
Login or Register to Ask a Question