capture the file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting capture the file name
# 1  
Old 05-25-2007
capture the file name

I am trying to capture the file name(which is not of fixed length) and put it in a variable. It is working in unix whereas when I am running the same script in Informatica it is not giving me the desired output. But when I comment the option(find the file name) then it is working fine. It can also because I am using awk. Is there any other way(other than using awk) to find the file name? File name will be in second line of the file with 2 other items filesize numeric(10) and record count numeric(8).

Here is my code:
#!/bin/ksh

eval $(awk 'NR==2 {
printf "header_fname=\"%s\"\n", substr($0,1,length-18) ## filename
}' $1)

echo "$header_fname"

Sample file:
id_xyz20070523085554004
abcdefgh.csv000000324100000036
# 2  
Old 05-25-2007
Code:
header_fname=`awk 'NR==2 { print substr($0,1,length-18) }' $1`
echo $header_fname

or
Code:
#!/bin/ksh
{
  read line
  read line
} < $1
header_fname=${line%??????????????????}
echo $header_fname

Jean-Pierre.
# 3  
Old 05-25-2007
Quote:
Originally Posted by Mandab
I am trying to capture the file name(which is not of fixed length) and put it in a variable. It is working in unix whereas when I am running the same script in Informatica it is not giving me the desired output. But when I comment the option(find the file name) then it is working fine. It can also because I am using awk. Is there any other way(other than using awk) to find the file name? File name will be in second line of the file with 2 other items filesize numeric(10) and record count numeric(8).

Here is my code:
#!/bin/ksh

eval $(awk 'NR==2 {
printf "header_fname=\"%s\"\n", substr($0,1,length-18) ## filename
}' $1)

echo "$header_fname"

Sample file:
id_xyz20070523085554004
abcdefgh.csv000000324100000036

In any BOURNE-type shell (bash, ksh, sh, etc.):

Code:
{
 read line
 IFS=0123456789 read header_fname junk
} < "$FILE"
echo "$header_fname"

# 4  
Old 05-26-2007
Quote:
Originally Posted by cfajohnson
In any BOURNE-type shell (bash, ksh, sh, etc.):

Code:
{
 read line
 IFS=0123456789 read header_fname junk
} < "$FILE"
echo "$header_fname"

Doesn't work if the file name contains any numeric character.

Jean-Pierre.
# 5  
Old 05-29-2007
Thank you Aigles, it is working perfectly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String capture from ip file

Dear All From below mention input file I want op file as mention. Kindly help. IP file: "BSCGNR4_IPA17_C" 329 140119 0717 RXOCF-105 KJO001_BASC_NG AC FAULTY DG ON DOOR OPEN Needed OP: 140119 0717 KJO001_BASC_NG AC FAULTY DG ON DOOR OPEN Note that string mark in red as variable in... (3 Replies)
Discussion started by: jaydeep_sadaria
3 Replies

2. Shell Programming and Scripting

Capture specific fields in file

Dear Friends, I have a file a.txt 1|3478.12|487|4578.04|4505.5478|rhfj|rehtire|rhj I want to get the field numbers which have decimal values output: Fields: 2,4,5 Plz help (6 Replies)
Discussion started by: i150371485
6 Replies

3. UNIX for Dummies Questions & Answers

Capture filenames with a Pattern into another file

Hi i have a command in windows which captures the filenames with the same pattern and route it to another file. for example filetest_20110921, filetest_20110922,filetest_20110923 etc I would like to know the command in UNIX/LINUX for the same. The command which i am using in Windows is ... (12 Replies)
Discussion started by: mansurkhalil
12 Replies

4. Shell Programming and Scripting

Capture first N Bytes from first line in a file

Hi Guyz, I need to capture first N Bytes from the first line of my file. Eg. If i have following data in File1 414d51204541495052475731202020204a910846230e420c Hello 3621363663212 Help Required Then, i want the value of first 48 Bytes to be stored in a variable. That is, variable... (5 Replies)
Discussion started by: DTechBuddy
5 Replies

5. Shell Programming and Scripting

capture the last file when you don't know the all name

I have to ftp a file from a directory in Windows, I need to be able to ftp the last file that was put in the directory, the only thing I know about that file is that the 4 first digits are always the same (3520) then it is a _5(more digits), random digits, so it will be something like this... (1 Reply)
Discussion started by: rechever
1 Replies

6. Shell Programming and Scripting

capture output of file and send last string thereof to new file

Hello, If I run a program from within shell, the output is displayed in the command line terminal. Is there a way I can capture that output and choose only the very last string in it to send it to a new file? Thank you (6 Replies)
Discussion started by: Lorna
6 Replies

7. Shell Programming and Scripting

Capture data in a file

How to capture the rows in a file after executing the store procedure using the perl scripts? (0 Replies)
Discussion started by: vinay123
0 Replies

8. Shell Programming and Scripting

Script to capture new lines in a file and copy it to new file

Hi All, I have a file that gives me new line/output every 5 minutes. I need to create a script that capture new line/output besides "IN CRON_STATUS", in this case the new output is "begin ------ cron_status.sh - -----------". I want this script to capture the line starting from "begin ------... (0 Replies)
Discussion started by: fara_aris
0 Replies

9. Solaris

capture ping to a file

I would like to capture the output from ping into a file. I am on solaris 9. I can get the basic "server is alive" command in a file, but I want the response from ping -s into a file. I am going to schedule this in cron to run at certain times during the day. When I run the ping -s in a... (3 Replies)
Discussion started by: MizzGail
3 Replies

10. UNIX for Advanced & Expert Users

Capture Value from file

I have a file in the following Format Fundid: 100-BankA AccountNumber Balance 1 200 2 300 3 400 FundId:123321-BankB AccountNumber Balance 1 200 3 100 ........... I can have N number of funds. (1 Reply)
Discussion started by: kris01752
1 Replies
Login or Register to Ask a Question