SED To insert Directory Contents to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED To insert Directory Contents to file
# 1  
Old 03-12-2007
SED To insert Directory Contents to file

I am on a HP-UX machine I have a directory called "/u01/blobs" and the files look like this:
ls -1
7398
7399
7400

I need to produce a comma delimited file with the following format:

filename,location/filename
i.e:
7398,/u01/blobs/7398
7399,/u01/blobs/7399
7400,/u01/blobs/7400

What I have so far is:
#!/bin/ksh
FILE_DIR=/u01/blobs
INFILE=a.txt
INFILE2=b.txt
INFILE3=c.txt

if [[ -a $INFILE ]]; then
rm $INFILE
fi

ls -1 $FILE_DIR > $INFILE

cat $INFILE | sed 's/$/,\/u01\/blobs\//w '${INFILE2} > /dev/null


But this just gives me the filename and directory.

It seams like a simple task but it is giving me a SEDache. Smilie

Regards,
NomDeGuerre
# 2  
Old 03-12-2007
Code:
$ find /u01/blobs -type f -print | while read filename; do
>    echo "${filename##*/},${filename}" 
> done > filelist
$ cat filelist
7398,/u01/blobs/7398
7399,/u01/blobs/7399
7400,/u01/blobs/7400

Cheers
ZB
# 3  
Old 03-13-2007
Give a try on this..
ls |sed 's!\([0-9]\{4\}\)!\1,/u01/blobs/\1!'

Output:
Code:
7395,/u01/blobs/7395
7399,/u01/blobs/7399
7400,/u01/blobs/7400

Thnx.Dennis
# 4  
Old 03-13-2007
Quote:
Originally Posted by jacoden
Give a try on this..
ls |sed 's!\([0-9]\{4\}\)!\1,/u01/blobs/\1!'

Output:
Code:
7395,/u01/blobs/7395
7399,/u01/blobs/7399
7400,/u01/blobs/7400

Thnx.Dennis
A slight modification..If you need it for any directory
dir_path=directory
ls $(dir_path) |sed 's!\([0-9]\{4\}\)!\1,'$dir_path'/\1!'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename file in directory using contents within each file

In the below there are two generic .vcf files (genome.S1.vcf and genome.S2.vcf) in a directory. There wont always be two genaric files but I am trying to use bash to rename each of these generic files with specfic text (unique identifier) within in each .vcf. The text will always be different, but... (11 Replies)
Discussion started by: cmccabe
11 Replies

2. Shell Programming and Scripting

How to insert file contents after nth occurrence of a string using sed?

Hi, I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g. > cat file1 banana apple orange apple banana pear tangerine apple > cat file2 I don't like apples What would be the sed command to insert... (5 Replies)
Discussion started by: dimocn
5 Replies

3. Shell Programming and Scripting

Concatenating contents of a file with members in a directory

Hi, I have a unix file with the below structure - CustId1 CustName1 CustPhn1 /u/home/xmldata/A000001 CustId2 CustName2 CustPhn2 /u/home/xmldata/A000002 CustId3 CustName3 CustPhn3 /u/home/xmldata/A000003 Then I have another unix directory /u/home/xmldata This directory has... (3 Replies)
Discussion started by: Simanto
3 Replies

4. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

5. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

6. Shell Programming and Scripting

Insert file contents into another file at a given position

I'm trying to write a small shell script/command to insert the contents of one file into another file at a position marked with a given text string. It's not necessarily at the top or bottom of the file so I can't just use cat to cat the files together. I think probably sed with the r option is... (5 Replies)
Discussion started by: shaun29
5 Replies

7. Shell Programming and Scripting

Copy contents of a directory only if a file exists

I'm looking to write a script that will check the contents of a directory, and if any files exist in that directory copy them to a temporary folder. The target files are only resident for a few seconds, so I think the script needs to be running constantly. Any pointers would be really... (3 Replies)
Discussion started by: danceofillusion
3 Replies

8. UNIX for Dummies Questions & Answers

Move contents of a directory into one file for e-mail distribution ...

Hello, Here is what I am trying to accomplish. I am going to have one directory in which there will be files of varying types (Excel, Word, PPT, and possible others), and I need to be able to be bundle however many files there are in there together in to one file to be used as an e-mail... (3 Replies)
Discussion started by: rip73
3 Replies

9. Shell Programming and Scripting

Replacing string in all instances (both filenames and file contents) in a directory

Hi, I have a set of files stored in a single directory that I use to set parameters for a physics code, and I would like to streamline the process of updating them all when I change a parameter. For instance, if the files are called A2000p300ini, A2000p300sub, A2000p300run, and the text in each... (3 Replies)
Discussion started by: BlueChris
3 Replies

10. Programming

How to read and write directory or file contents in c++ ?

Dear Friends, I m beginner unix programmer. I want to know, how to read and write directory or file contents in c++ ? (3 Replies)
Discussion started by: namrata5
3 Replies
Login or Register to Ask a Question