Create 2 Columns from filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create 2 Columns from filename
# 1  
Old 12-04-2017
Create 2 Columns from filename

Hi, I have a directory of files with filenames all in the format of xxx_yyy_zzz.pdf
Where xxx and yyy changes each filename but zzz stays the same. For example: File 1: Gold_Car_Vehicle.pdf; File n: Red_Truck_Vehicle.pdf.

I need to cycle through each file and output a Text File with 2 columns. The two columns are xxx and the complete name of the pdf.

So, in the case of the two Files above, my text file would read:


Code:
Column 1            Column 2
Gold            Gold_Car_Vehicle.pdf
Red             Red_Truck_Vehicle.pdf

So far I have the script:

Code:
#!/bin/sh

set -e
for f in *.pdf
do
  d=${f%_*.*}
  for drt in $(IFS=_;echo $d)
  do
    d=${f%_*.*}
    if [ ! -d "$drt" ]; then
      echo "$drt"
      echo "$f"
    fi
  done
done

What am I missing?

Last edited by Scott; 12-04-2017 at 03:12 PM.. Reason: Please use code tags
# 2  
Old 12-04-2017
This should work:-
Code:
for file in *.pdf
do
        echo "${file%%_*} $file"
done

# 3  
Old 12-04-2017
Thanks Yoda, that worked!

Last edited by ndnkyd; 12-04-2017 at 03:38 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Create file with last month in filename

Hi, I have a bash script which outputs a file with current year and month in the filename. Last line is: cat result_* > output.$(date +%y%m) So for Feb 2013 this gives me output.1302 Now I am requested to run the script every first day of the month, but with the previous month in... (4 Replies)
Discussion started by: viscacha
4 Replies

2. Shell Programming and Scripting

How to create a cron job to take an uploaded filename and move it?

OK, So complete newbie here. I would normally do this in PHP or through my FTP program script but I can't in this case (the files are not coming from me, coming from a third party FTP upload). I have an FTP server (Linux) accepting files coming in from a standard FTP program. Each file... (2 Replies)
Discussion started by: bull_frog
2 Replies

3. Shell Programming and Scripting

Merge CSV files and create a column with the filename from the original file

Hello everyone!! I am not completely new to shell script but I havent been able to find the answer to my problem and I'm sure there are some smart brains here up for the challenge :D. I have several CSV files that I need to combine into one, but I also need to know where each row came from.... (7 Replies)
Discussion started by: fransanchezoria
7 Replies

4. Shell Programming and Scripting

create new column for filename

Hi, I created a list with 2 columns. Each line is from a different file. I am getting these with a loop in Perl. I would like to add a 3rd column with the name of the file that the line is coming from. I usually use pr to print the filename but this is not working here ... I was wondering if... (5 Replies)
Discussion started by: danieladna
5 Replies

5. UNIX for Dummies Questions & Answers

shell scripts - create a filename with the date appended

I am looking to do something where if I created a file named backup,or whatever it would print a name like “backup_Apr_11_2011”. Thanks citizencro (1 Reply)
Discussion started by: citizencro
1 Replies

6. Shell Programming and Scripting

Extract date from filename and create a new file

Hi, i have a filename CRED20102009.txt in a server 20102009 is the date of the file ddmmaaaa format the complete route is /dprod/informatica/Fuentes/CRED20102009.csv i want to extract the date to create a new file named Parameters.txt I need to create Parameters.txt with this... (6 Replies)
Discussion started by: angel1001
6 Replies

7. Shell Programming and Scripting

how to create variables in loop and assign filename after set command?

Hi, does anybody knows how to manage, that the filenames are assigned to a variable in a loop afer getting them with set command in a ksh, like: set B*.txt i=1 c=$# x=$((c+1)) echo "$x" while ] ; do _ftpfile$i="$"$i echo "$_ftpfile$i" i=$((i+1)) done The first echo returns,... (2 Replies)
Discussion started by: spidermike
2 Replies

8. Shell Programming and Scripting

extract columns from 2 different files and create new file

Hi All, I have 2 issues while working with file. 1. I have 2 delimited(~) files. I want to extract column numbner 3 from file1 and column number 8 from file2 and paste it into file3. I have tried using cut, but not able to get answer. 2. i have 2 filxed-width file. I wanted to do same... (1 Reply)
Discussion started by: Amit.Sagpariya
1 Replies

9. Shell Programming and Scripting

read mp3 filename and create one XML for each file

Hi: I have a collection of mp3s and I need to create 1 xml file per mp3. I have: recording1.mp3 recording2.mp3 etc and I want to generate this kind of files. recording1.xml recording2.xml and inside each xml file I need to add a url prefix and then the filename at the end. ... (4 Replies)
Discussion started by: jason7
4 Replies

10. Shell Programming and Scripting

create filename with 'DD/MM/YYYY' date format

Hi, I can use the following command to create a file with some name then underscore and then date appended to it in the format 'DD-MM-YYYY': touch "newfile_`date '+%d-%m-%Y'`" But it gives me error when I try with the similar command to create a file with the date format 'DD/MM/YYYY'. I... (4 Replies)
Discussion started by: royalibrahim
4 Replies
Login or Register to Ask a Question