insert part of a filename in a textfile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting insert part of a filename in a textfile
# 1  
Old 07-12-2012
insert part of a filename in a textfile

Hi All,

I've got a textfile that i've stripped and edited using sed and want to insert part of the filename (original filename consists of: fw0204.txt.ncat_report.txt) but there are multiple files being delivered by an application to the data directory

The part of the filename that I need to insert is fw0204 with a whitespace at the end of it but I don't want to incorporate 'fw0204 ' in the script but instead use a general operator to do:

Work on fw0204.txt.ncat_report.txt and insert 'fw0204 ' in front of each line(without the quotes)
Work on fw0305.txt.ncat_report.txt and insert 'fw0305 ' in front of each line(without the quotes)
etc...

What I've done in the script with removing all kinds of things is:

Code:
# fill a container file with the available filenames
ls *.txt > filename_txt

# remove the first line of the textfile which contains a header
for x in `cat filename_txt`
do
sed '1d' $x > temp01_txt
cat temp01_txt > $x
done

#remove last 20 lines containing comments
for x in `cat filename_txt`
do
sed -e :a -e '$d;N;2,20ba' -e 'P;D' $x > temp02_txt
cat temp02_txt > $x
done

#remove trailing characters from each line (at the end)
for x in `cat filename_txt`
do
echo 'fw' | sed -e 's/fw.*//' $x > temp03_txt
cat temp03_txt > $x
done

Thanks in advance!

Last edited by Franklin52; 07-12-2012 at 04:50 PM.. Reason: Please use code tags for data and code samples, 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

Finding the part of a filename

Hi, I am writing an ebuild for Gentoo Linux operating system. Writing an ebuild is about Bash scripting where I am a newbie. So, my ebuild must find a part of a specific filename. Such a filaname my look like this: libvclient_release_x64.so.740and I must to find the number at the and of... (18 Replies)
Discussion started by: csanyipal
18 Replies

2. Shell Programming and Scripting

How to separate sorte different characters from one textfile and copy them in a new textfile?

My first post, so don't kill me :) Say i open some textfile with some example like this. on the table are handy, bread and wine Now i know exactly what is in and i want to separate and sorted it in terminal to an existing file with another 2 existing lines in like this: table plane ... (3 Replies)
Discussion started by: schwatter
3 Replies

3. Shell Programming and Scripting

Insert FileName to headers

hi there I have a lot of files with same number of rows and columns.$2 and $3 are the same in all files . I run awk script and merge $2,$3,$1 from first file and $1 from another files. I would like to know how to modify awk script to insert filename as header for merged columns ( Expected... (6 Replies)
Discussion started by: ali.seifaddini
6 Replies

4. Shell Programming and Scripting

Insert Filename into Multiple Files

Hi, We have a folder that has files in the following structure abc.sql def.sql efg.sql . . . xyz.sql I have to find a certain string (say "test") in each file and replace it with the name of the file. For eg. if "test" is present in abc.sql, I want to replace it with "test abc". If... (8 Replies)
Discussion started by: jerome_rajan
8 Replies

5. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

6. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

7. Shell Programming and Scripting

cut the some part in filename

Hi All, I have the file & name is "/a/b/c/d/e/xyz.dat" I need "/a/b/c/d/e/" from the above file name. I tryning with echo and awk. But it not come. Please help me in this regard. Thanks & Regards, Dathu (3 Replies)
Discussion started by: pdathu
3 Replies

8. Shell Programming and Scripting

Getting part of a filename

Hi All, I'm trying to get part of a filename and my skill with regular expression are lacking. I know I need to use SED but have no idea how to use it. I'm hoping that someone can help me out. The file names would be: prefix<partwewant>suffix.extension the prefix and suffix are always 3... (4 Replies)
Discussion started by: imonkey
4 Replies

9. Shell Programming and Scripting

part of a filename

Hi, I need to extract only a part of the filenames of some files. The files are named this way : .tap_profile_SIT02 I want the "SIT02" part, which is not the same for each file. I was able to get what I want with bash, but not with ksh. Here is the command I used in bash : find... (8 Replies)
Discussion started by: flame_eagle
8 Replies

10. UNIX for Dummies Questions & Answers

Insert date/time within a filename

Hi Guys, I need to script the renaming of files as followins: files: firstjd secondjo thirdjv My script needs to insert the date/time infront of the last 2 characters of the filenames above, any ideas greatly received :) the letters before the last 2 characters could change, I'm only... (7 Replies)
Discussion started by: cooperman
7 Replies
Login or Register to Ask a Question