Scripting help- cut the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripting help- cut the string
# 1  
Old 10-15-2010
Question Scripting help- cut the string

Hi
I am having a file with the following content(its created by ls of a directory):
-rw-r----- 1 321 321 0 Oct 14 10:41 xcv23456
-rw-r----- 1 321 321 0 Oct 14 10:41 xcv23457
-rw-r----- 1 321 321 0 Oct 14 10:41 xcv23458
-rw-r----- 1 321 321 0 Oct 14 12:19 xcv23459
-rw-r----- 1 321 321 0 Oct 15 11:19 xcv23460

last column ie 9th column contains the file names.
I am searching for a script which can cut the last 5 digits of each row and store it in a seperate variable.

Thnks in advance
# 2  
Old 10-15-2010
The key here is to use "awk" to extract the filename rather than "cut" because when the day of the month in the timestamp is a single digit the filename will be in a different position.

Code:
0ne way:
cat myfile.txt | awk '{print $9}' | cut -c4-8 | while read number
do
       echo "${number}"
done

Or if you prefer redirection to cat:
cat myfile.txt | awk '{print $9}' | cut -c4-8 | while read number
do
       echo "${number}"
done < myfile.txt

Or we could read the file directly with "awk":
awk '{print $9}' myfile.txt| cut -c4-8 | while read number
do
       echo "${number}"
done


Last edited by methyl; 10-15-2010 at 09:42 AM.. Reason: paste mistake
This User Gave Thanks to methyl For This Post:
# 3  
Old 10-15-2010
Code:
$ ruby -ne 'puts $_[-6..-1]' file
23456
23457
23458
23459
23460

# 4  
Old 10-15-2010
Thnaks Smilie it works
# 5  
Old 10-15-2010
assuming your fields have fixed width ...

Code:
cut -c 40- yourfile | while read line ; do ; echo "num=$line" ; done

# 6  
Old 10-15-2010
Code:
$
$ cat f27
-rw-r----- 1 321 321 0 Oct 14 10:41 xcv23456
-rw-r----- 1 321 321 0 Oct 14 10:41 xcv23457
-rw-r----- 1 321 321 0 Oct 14 10:41 xcv23458
-rw-r----- 1 321 321 0 Oct 14 12:19 xcv23459
-rw-r----- 1 321 321 0 Oct 15 11:19 xcv23460
$
$
$ perl -plne 's/^.*(.{5})$/$1/' f27
23456
23457
23458
23459
23460
$
$
$ perl -plne '$_=substr($_,-5)' f27
23456
23457
23458
23459
23460
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 7  
Old 10-19-2010
Thanks everyone. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut the string

---------- Post updated at 10:31 AM ---------- Previous update was at 10:28 AM ---------- Hello, I am trying to get the string cut based on the following needs: String1=Submitted request 25574824 for CONCURRENT SQLAP RUAPACTUALSEXT Y RUAPACTUALS122313100616.dat "2013/01/12 14:50:44"... (6 Replies)
Discussion started by: cartrider
6 Replies

2. Shell Programming and Scripting

Cut the string

Hi in a directory i've files having the following name for_category_info_19990101984301 for_catgry_meta_19991111214601 ------- I just want the name till year and month i.e; for_category_info_199901 for_catgry_meta_199911 How can i achieve the above string Thanks (2 Replies)
Discussion started by: smile689
2 Replies

3. Shell Programming and Scripting

Shell scripting- cut the location of the file

Hi, I need to just cut the location of file and give it as input to the other command. For example: ct lsview | grep kavya- * kavya-telecom-view /first/nso01/disk2/views/kavya-telecom-view.vws I need only the location like /first/nso01/disk2/views/kavya-telecom-view.vws.How to cut that... (4 Replies)
Discussion started by: kkalyan
4 Replies

4. UNIX for Dummies Questions & Answers

how to cut based on a string

Hi I have some data where each line will look something like this: Time, name, i.d number, RB: 0.9949; RMQA: 0.0005; RB: 0.9951; RRA: 0.3; RA: 0.995; RA: 0.996; EA: 0.99105 etc. I want to cut out all the RB: and RA:'s with the numbers after. so in the above example i'd be left... (3 Replies)
Discussion started by: gvc
3 Replies

5. UNIX for Dummies Questions & Answers

How to cut a string from a variable

I want to cut column 1 to 5 echo $somevariable | cut -c 1-5 not working (6 Replies)
Discussion started by: ezee
6 Replies

6. UNIX for Dummies Questions & Answers

How to cut data block from .txt file in shell scripting

Hi All, Currently i have to write a script. For which i need to cut a block from .txt file. I know the specific word that starts the block and ends the block. Can we do it in shell scripting..? Please suggest.... (6 Replies)
Discussion started by: pank29
6 Replies

7. UNIX for Dummies Questions & Answers

how to cut prefix from a string

I have a file: chromosome1:436728 chromosome2:32892 ..... chromosome22:23781 I just want to get the number, not the prefix "chromosomeX", so I want to remove all the prefix ahead of the numbers. How can I do that?? Thanks!!! (PS: give me some very simple command so that I can understand... (4 Replies)
Discussion started by: kaixinsjtu
4 Replies

8. Shell Programming and Scripting

cut First charecter in any string

I wannt to cut first char of any string. str=A2465443 out put will be. str1=A str2=2465443. I tried str2=${?%srt} str1=${str#$str2} it is not working. (6 Replies)
Discussion started by: rinku
6 Replies

9. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

10. UNIX for Dummies Questions & Answers

how to cut a string from a variable

Hello All, I have a string type variable called "FULLSTR". This variable has a value of "path=/type/source/logs". I need to cut the characters after the character "=" to end of the string. Example: the new variable "MATCHSTR" should have value like this... "/type/source/logs" So... (2 Replies)
Discussion started by: kjaisan
2 Replies
Login or Register to Ask a Question