awk: isolate a part of a file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: isolate a part of a file name
# 1  
Old 12-16-2010
awk: isolate a part of a file name

hi there,

i have a file named 'x20080613_x20100106.pwr1.gc', i want to isolate the part 'x20080613_x20100106' but by using the following line i isolate the part '.pwr1.gc':

HTML Code:
`awk '$0=substr($0, length($0)-7)' $temp`
how can i reverse that?
thank you!
# 2  
Old 12-17-2010
So many ways to do this:

With awk:
Code:
 echo 'x20080613_x20100106.pwr1.gc' | awk '$0=substr($0, 0, length($0)-8)'

With ksh/bash
Code:
$ F='x20080613_x20100106.pwr1.gc'; echo ${F%%.*}

With sed
Code:
echo 'x20080613_x20100106.pwr1.gc' |  sed 's/........$//'

With sed and extended regular expressions
Code:
echo 'x20080613_x20100106.pwr1.gc' |  sed -r 's/.{8}$//'

# 3  
Old 12-17-2010
An easier way using awk as you requested:
Code:
echo 'x20080613_x20100106.pwr1.gc' | awk -F. '{ print $1 }'

Or to follow the best practice of using the most simple tool for the job, use parameter expansion as mentioned by Chubler_XL:
Quote:
$ F='x20080613_x20100106.pwr1.gc'; echo ${F%%.*}
Another easy way is with cut:
Code:
echo 'x20080613_x20100106.pwr1.gc' | cut -d. -f1

# 4  
Old 12-17-2010
Bug

thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Strip the first record off a file and isolate fields off of it

I have a 2 part question on how to this in unix scripting using kshell or c shell. I have a file described below: 1st record has 2 fields on it every other record has 22 fields on it. Example ABC, email address Detail 1 Detail 2 Detail 3 . . . 1st question is... (4 Replies)
Discussion started by: jclanc8
4 Replies

2. UNIX for Dummies Questions & Answers

I need to isolate a date in a large log file

I wrote head -n1 example.log I grab the first line of the log, but I need to isolate just the date, which is 08/May/2012:09:52:52. I also need to find the reverse of this, which would be tail... http://i.imgur.com/Lp1eBD0.png Thanks in advance (4 Replies)
Discussion started by: spookydll
4 Replies

3. Shell Programming and Scripting

Store and isolate bad pages from a file to new file

I have a file like below . The good pages must have 3 conditions : The pages that containing page total only must have 50 lines. The pages that containing customer total only must have 53 lines. The last page of Customer Total should be the last page. How can I accomplish separating good... (1 Reply)
Discussion started by: ehabaziz2001
1 Replies

4. Shell Programming and Scripting

[awk] grep a part of filename as entry file

hi all, i need to combine these files into one csv file. Bounce_Mail_Event_Daily_Report_01_Jul_2012.csv Bounce_Mail_Event_Daily_Report_02_Jul_2012.csv Bounce_Mail_Event_Daily_Report_03_Jul_2012.csv Bounce_Mail_Event_Daily_Report_04_Jul_2012.csv... (10 Replies)
Discussion started by: makan
10 Replies

5. Shell Programming and Scripting

Grep to isolate a text file line and Awk to select a word?

I am looking at using grep to locate the line in the text file and them use awk to select a word or words out of it. I know awk needs -v to allow a variable to be used, but also needs -F to allow the break up of the sentence and allow the location of separate variables. $line = grep "1:" File |... (8 Replies)
Discussion started by: Ironguru
8 Replies

6. Shell Programming and Scripting

Using awk to isolate specific rows

Hi all! Let's say I have obtained this dataset from the ISI Web of Knowledge ... PT J AU Yousefi, Ramin Muhamad, Muhamad Rasat Zak, Ali Khorsand TI The effect of source temperature on morphological and optical properties of ZnO nanowires grown using a modified thermal... (17 Replies)
Discussion started by: sidiqmk
17 Replies

7. Shell Programming and Scripting

search needed part in text file (awk?)

Hello! I have text file: From aaa@bbb Fri Jun 1 10:04:29 2010 --____OSPHWOJQGRPHNTTXKYGR____ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline My code '234565'. ... (2 Replies)
Discussion started by: candyme
2 Replies

8. Shell Programming and Scripting

Perl or Awk script to copy a part of text file.

Hi Gurus, I'm a total newbie to Perl and Awk scripting. Let me explain the scenario, there is a DB2 table with 5 columns and one of the column is a CLOB datatype containing XML. We need all the 4 columns but only a portion of string from the XML column. We decided to export DB2 table to a .del... (26 Replies)
Discussion started by: asandy1234
26 Replies

9. UNIX for Dummies Questions & Answers

sed to isolate file paths separated by a pattern

Hi, I've been searching for a quick way to do this with sed, but to no avail. I have a file containing a long series of (windows) file paths that are separated by the pattern '@'. I would like to extract each file path so that I can later assign a variable to each path. Here is the file:... (2 Replies)
Discussion started by: nixjennings
2 Replies

10. UNIX for Dummies Questions & Answers

how can i isolate the random sequence of numbers using awk?

as you can see there is a delimiter after c8 "::". Awk sees the rest as fields because it doesn't recognize spaces and tabs as delimiters. So i am basically looking to isolate 20030003ba13f6cc. Can anyone help? c8::20030003ba13f6cc disk connected configured unknown (2 Replies)
Discussion started by: rcon1
2 Replies
Login or Register to Ask a Question