Getting the folder name and file name after delimiter


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Getting the folder name and file name after delimiter
# 1  
Old 06-14-2013
Getting the folder name and file name after delimiter

Hi,

I have a input

Code:
/dev/cm/test1.txt
/qa/tm/hmkr/cc/test2.txt

and I need an out like below

foldername, filename

Code:
/dev/cm/,test1.txt
/qa/tm/hmkr/cc/,test2.txt

I tried with awk $NF, but I'm getting the filenames and not folder names. Please let me know how to achive the above result.

Thanks,
Raju

Last edited by Scott; 06-14-2013 at 06:30 PM.. Reason: Code tags, please...
# 2  
Old 06-14-2013
You can do this using parameter expansion, if your shell supports it:

Code:
while read LINE; do
  echo ${LINE%/*}/,${LINE##*/}
done < file

/dev/cm/,test1.txt
/qa/tm/hmkr/cc/,test2.txt

# 3  
Old 06-15-2013
I tried with awk command

Code:
head -10 /home/o55555/test11.txt | awk -F '/' '{print $0\%/*","$NF}' .

But I got below error
Code:
Syntax Error The source line is 1.
The error context is
{print >>> $0\ <<< %/*","$NF}
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.

and the file has the data like I mentioned in my early post
Code:
/dev/cm/test1.txt
/qa/tm/hmkr/cc/test2.txt

Can we get above result using awk command

Last edited by Scott; 06-15-2013 at 09:13 AM.. Reason: Code tags
# 4  
Old 06-15-2013
Quote:
Originally Posted by somu_june
I tried with awk command

head -10 /home/o55555/test11.txt | awk -F '/' '{print $0\%/*","$NF}' .

But I got below error Syntax Error The source line is 1.
The error context is
{print >>> $0\ <<< %/*","$NF}
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.

and the file has the data like I mentioned in my early post

/dev/cm/test1.txt
/qa/tm/hmkr/cc/test2.txt

Can we get above result using awk command
You can get the above result for this exact input. But, to get a reliable solution we need more details about what the what forms of pathnames can exist in your input file, what shell you're using, and whether or not your environment supports a super root file system.

If the contents of your input file only contains absolute pathnames, never contains the pathname "/", and either your environment doesn't support a super root or your input file will never contain the pathname of a file in your root directory; then the solution Scott proposed will work and can be translated into awk code if you really need something much less efficient.

If your input contains any relative pathnames that don't contain a / character, we need a completely different approach.

If your environment includes a super root (where //node refers to the root of the system named node on your local network), there is another set of issues that need to be considered before we can propose an approach that will work reliably.

So, instead of giving us a two line example of what the input might look like, please give us an explicit description of the allowed input file format.
# 5  
Old 06-15-2013
awk does not extract ranges of fields that easily like you wanted ... you will need to make your awk script longer or you can pipe your head output to what scott wrote (see sample 1) or do the longer way using other utilities (see sample 2) ...

Sample 1:
Code:
head -10 /home/o55555/test11.txt | while read LINE; do
  echo ${LINE%/*}/,${LINE##*/}
done

Sample 2:
Code:
head -10 /home/o55555/test11.txt | while read line
do
     dir=$(dirname $line)
     file=$(basename $line)
     echo "$dir,$file"
done

# 6  
Old 06-15-2013
A simple way to append a comma after the final slash (if any) or the beginning of the line:
Code:
sed 's|[^/]*$|,&|'

Regards,
Alister
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

2. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

3. UNIX for Advanced & Expert Users

File Delimiter

Hi All, I woul like to know with out opening a file in unix ,how we can find out what is the delemeter in that file... Thanks.. edit by bakunin: changed thread title to "delimiter" so it can be found. (4 Replies)
Discussion started by: raju4u
4 Replies

4. Shell Programming and Scripting

How to cut by delimiter, and delimiter can be anything except numbers?

Hi all, I have a number of strings like below: //mnt/autocor/43°13'(33")W/ and i'm trying to get the numbers in this string, for example 431333 please help thanks ahead (14 Replies)
Discussion started by: sunnydanniel
14 Replies

5. Shell Programming and Scripting

Delimiter in output file

Hello, I am trying to find the record count in a specific folder, Here is the part of the code =========================== STARTDATE=`date +"%y%m%d%H%M"` for i in `ls *.DAT` do wc -l $i >> /XYZ/SrcFiles/"Record_counts"$STARTDATE.csv ... (2 Replies)
Discussion started by: Shanks
2 Replies

6. Shell Programming and Scripting

want to move set of file from one folder to another folder

Hi all, let me explain my requirments i am having 5 folder with different name for eg) abc , cdf , efd, rtg, ead each 5 folders contain 15 files i want to move 10 files to some other folder, remain 5 files should be there in the same folder. give me some suggestion on this. (6 Replies)
Discussion started by: natraj005
6 Replies

7. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies

8. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

9. Shell Programming and Scripting

Move the file from one folder to another folder

Hi, I have a requirement to move a file from one folder(a) to another folder(b) only when folder (b) have a write permission. Folder permission is 755 If the permission is otherthan 755 we need to come out of the loop I will appreciate your help Thanks Soll (1 Reply)
Discussion started by: sollins
1 Replies

10. Shell Programming and Scripting

Parse the .txt file for folder name and FTP to the corrsponding folder.

Oracle procedure create files on UNIX folder on a regular basis. I need to FTP files onto windows server and place the files, based on their name, in the corresponding folders. File name is as follows: ccyymmddfoldernamefile.txt; Folder Name length could be of any size; however, the prefix and... (3 Replies)
Discussion started by: MeganP
3 Replies
Login or Register to Ask a Question