Visit Our UNIX and Linux User Community


Getting a file name from input file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting a file name from input file
# 1  
Old 08-19-2013
Getting a file name from input file

Hi

suppose I run my script using an argument (abc.xyz)
i want to strip the extension off nad just use the file name for further manipulations.

any ideas ?


~thanks
# 2  
Old 08-19-2013
Code:
echo "abc.xyz" | awk -F. '{print $1}'
abc

# 3  
Old 08-19-2013
Code:
echo ${1%.*}

where $1 is the first argument passed to the script. If you are storing the argument in a variable, you can use

Code:
echo ${FILENAME%.*}

where FILENAME is the variable which stores the argument passed.
# 4  
Old 08-19-2013
how about this ??
Code:
fullname="$1"
echo $fullname
name= "$fullname" | cut -f1 -d"."
echo $name


echo fullname works
bt cut command isnt working

what do i do ?

---------- Post updated at 03:23 PM ---------- Previous update was at 03:19 PM ----------

and what about this ??

malo901_cp-link_0807_020.csv

what If I want only cp-link out of it ??

Last edited by Franklin52; 08-19-2013 at 08:30 AM.. Reason: Please use code tags
# 5  
Old 08-19-2013
Replace name= "$fullname" | cut -f1 -d"." with

Code:
name=$(echo "$fullname" | cut -f1 -d".")

---------- Post updated at 03:27 PM ---------- Previous update was at 03:24 PM ----------

Code:
echo "malo901_cp-link_0807_020.csv" | cut -d"_" -f 2

# 6  
Old 08-19-2013
Thanks all Smilie
# 7  
Old 08-19-2013
There is a simpler way

If you always know the extension and if it static..says extension is always xyz then you can use 'basename'

>basename abc.xyz .xyz
abc

Previous Thread | Next Thread
Test Your Knowledge in Computers #804
Difficulty: Medium
Statistical signal processing is an approach which processes signals as stochastic processes, utilizing their statistical properties to perform signal processing tasks.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Avoid overwriting backup file when multiple entries need to replace in one file input from another

Hello, I have been working on script in which search and replace the multiple pattern. 1. update_params.sh read the multiple pattern from input file ParamMapping.txt(old_entry|New_entry) and passing this values one by one to change_text.sh 2. change_text.sh read... (0 Replies)
Discussion started by: ketanraut
0 Replies

4. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

5. Homework & Coursework Questions

Removing punctuations from file input or standard input

Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints. 1. The problem statement, all variables and given/known data: How do I write a shell script that takes in a file or... (4 Replies)
Discussion started by: fozilla
4 Replies

6. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

7. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

8. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

9. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

10. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

Featured Tech Videos