Conditionally prepending text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditionally prepending text
# 1  
Old 07-04-2010
Question Conditionally prepending text

I am currently writing a script to compare a file list created over an FTP connection to a local directory.
I have cleaned the FTP file list up so that I just have a raw list of filenames however due to the directory structure employed (both locally and on the ftp site) I need to prepend each line with a directory name based on the first letter of the filename. For filenames that are numeric, the directory name is '123'. From this, I can perform a diff in order to give me a manifest of files missing locally to be downloaded using curl or wget.

Suggestions please?
# 2  
Old 07-04-2010
Post sample input and desired output.
# 3  
Old 07-04-2010
Right ok so I have a list of filenames:

100.txt
123.txt
297.txt
597.txt
abc.txt
bcd.txt
fgh.txt
xyz.txt

Desired output:
/123/100.txt
/123/123.txt
/123/297.txt
/123/597.txt
/a/abc.txt
/b/bcd.txt
/f/fgh.txt
/x/xyz.txt
# 4  
Old 07-04-2010
Hi

Code:
sed -e 's|^[0-9]|/123/&|' -e 's|^[a-z]|/&/&|'   infile

where infile is the file containing list of filenames.

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 07-04-2010
Code:
sed 's|.|/&/&|;s|^/[0-9]|/123|' infile

Smilie
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 07-04-2010
Code:
perl -plne '/^(.)/ and $x=$1; $x =~ /\d/ ? s/^/\/123\// : s/^/\/$x\//' infile

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 7  
Old 07-06-2010
I was messing about with awk, but sed is definitely a neater solution IMHO.

Thanks all!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed script for appending & prepending

Hello Mates I am trying to write a script, which appends and prepends the text in a file. I tried testing with a small file and it worked fine. but for the large file, the script is wiping the entire file and adds only the word to be appended in the file. mv $file_name $file_name.bak sed... (6 Replies)
Discussion started by: sathyaac
6 Replies

2. Shell Programming and Scripting

[Shell/Perl(?)] Prepending timestamps to console output & writing results to a file

I do a lot of TSM work and I embarked on what I thought would be an easy task, and I'd be very happy for any input to save the pounding my keyboard is receiving :] By default, the output of TSM's console has no timestamping, making it hard to sort through accurately. This puts my console into... (5 Replies)
Discussion started by: Vryali
5 Replies

3. Shell Programming and Scripting

How to conditionally replace a pattern?

Hi, How to replace only the function calls with a new name and skip the function definition and declarations. consider the following code. There are 2 functions defined here returnint and returnvoid. I need to replace returnint with giveint and returnvoid with givevoid only in the function... (2 Replies)
Discussion started by: i.srini89
2 Replies

4. Shell Programming and Scripting

How to replace a text in a file conditionally?

I have got about 100 ascii files and I want replace some variable with a new one on an HP-UX system. But I want to put a line of comments before the change. I want to make file1 to file2. I am explaining below. file1: line1 line2 export QNAME=ABC line4 line5 file2: line1 line2 #... (3 Replies)
Discussion started by: asutoshch
3 Replies

5. UNIX for Dummies Questions & Answers

Prepending lines with file name

I have a number of dat-files, such as abc.dat, def.dat etc, as follows: 2011-07-01 100.0 2011-07-02 101.0 2011-07-03 101.7 I want to prepend the file with the base file name; so for abc.dat it would result in the following: abc 2011-07-01 100.0 abc 2011-07-02 101.0 abc 2011-07-03 ... (5 Replies)
Discussion started by: figaro
5 Replies

6. UNIX for Dummies Questions & Answers

Prepending information from filenames into files

I would like to know how to take information from a filename and place it into the text of the same file. Let's say I have a file called height_2_width_1.txt containing data that is related to a height of 2 and a width of 1, and the text originally looks like this: where these two columns... (13 Replies)
Discussion started by: Scatterbrain26
13 Replies

7. Shell Programming and Scripting

conditionally combine text from two files into one

Hi! I'm trying to take multiple text files (6), which have text on some lines but not others, and combine them. I'd also like to make the values in one column of some of the files (files 4-6) negative. I'm trying to write a short script (see below) as I have to do this with a large number of... (2 Replies)
Discussion started by: felix.echidna
2 Replies

8. Shell Programming and Scripting

Redirection after prepending timestamp

Hi all, I have little experience with Scripting so hoping someone may be able to help me or point me in the right direction. I have a shell script which was outputting uncaught exceptions to a log file. $JAVA_MAIN_CLASS > $LOGNAME 2>&1 What I want to be able to do is prepend a timestamp on... (8 Replies)
Discussion started by: MacAonghusa
8 Replies

9. Shell Programming and Scripting

Add text at the end of line conditionally

Hi All, I have a file as below: cat myfile abcdef NA rwer tyujkl na I wish to add the text ".txt" at the end of all lines except the lines starting with NA or na. I know i can add text at the end of line using following command but I am not sure how to valiate the condition. (14 Replies)
Discussion started by: angshuman
14 Replies

10. Shell Programming and Scripting

Finding pattern & prepending a line with text

Hello Dudes, I have a task to make a unix shell script that should search for a specific TEXT in a file.If that TEXT is found, shell script should add a comment statement before that TEXT line. Ex : LINE 1 xxxxx LINE 2 xxxx CALL xxxx LINE 3 xxxx PERFORM UNTIL if i... (1 Reply)
Discussion started by: kirrushna
1 Replies
Login or Register to Ask a Question