split a filename and print to 2 different headings


 
Thread Tools Search this Thread
Operating Systems AIX split a filename and print to 2 different headings
# 1  
Old 10-13-2008
split a filename and print to 2 different headings

I need help to split a filename 'a0crk_user:A0-B0123$#%test' into a0crk_user and A0-B0123 and print the output under 2 different columns namely
User and Status.
for eg. the output should like below:
User Status
---- ------
a0crk_user A0-B0123
# 2  
Old 10-13-2008
There are several possibilities to achieve this: all these filenames are of the form

<first_part><delimiter><second_part>

and you want to split them at <delimiter>. This chan be achieved by:

1. using shell means

${varname#*<delimiter>} will expand to the part of varname following delimiter
${varname%<delimiter>*} will expand to the part of varname preceeding delimiter

Example:
Code:
a="abc|def"
print - ${a#*|}    # will yield "def"
print - ${a%|*}    # will yield "abc"

2. using cut

You can use "cut" to split a string at some "field boundaries" delimited by a delimiter character. Quite commonly this is a blank but this doesn't have to be so. See the manpage for "cut" for details:

Code:
a="abc|def"
print - $a | cut -d'|' -f1    # will yield "abc"
print - $a | cut -d'|' -f2    # will yield "def"

This would be the preferential method when you have to split your variable not one but several times. In this case you could split to "field" 3,4,5, etc..

3. using sed/awk

You could use sed or awk to split your variables content into parts. This would be the least preferable method as it would be an overkill for such a problem.

Code:
a="abc|def"
print - $a | sed 's/|.*$//'    # will yield "abc"
print - $a | sed 's/^.*|//'    # will yield "def"

After having split your variables into parts you can use "printf" to print the parts as column headers in a table. "printf" has also a manpage and works almost identical to the standard C function printf().

I hope this helps.

bakunin
# 3  
Old 10-13-2008
Other possibilities. With sed:

Code:
sed 's/\(.*\):\(.*\)\$.*/User status\n---- ----\n\1 \2/'

With awk:

Code:
awk -F"[:$]" '{printf("User Status\n---- ----\n%s %s",$1,$2)}'

If you get errors with awk use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 4  
Old 10-15-2008
Thanks to both of you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk, split, print

How to print the split array elements in the same line with awk? echo "1 2 3 4 /path/to/file1" | awk 'split($5, A, "/") {print $0; for (i=1; i<=length(A); i++) print A}' echo "2 2 3 6 /longer/path/to/another/file2" | awk 'split($5, A, "/") {print $0; for (i=1; i<=length(A); i++) print A}' What... (6 Replies)
Discussion started by: yifangt
6 Replies

2. Shell Programming and Scripting

perl script to split the filename

In PERL script I have few files named theme1.htm,theme2.htm,theme3.htm and so on. now I need to write perl code to split the the filename and store only that particular digit. Example -------------- filename is theme1.htm output should be 1 another example ---------------... (5 Replies)
Discussion started by: giridhar276
5 Replies

3. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

4. Shell Programming and Scripting

Print filename with awk

I can got the filename with this script. it's only show "-" in result. cut -d , -f7 CSV_d.* | awk 'OFS=":"{print FILENAME,substr($1,1,8),substr($1,9,2),substr($1,11,2),substr($1,13,2)}' | sort |uniq (2 Replies)
Discussion started by: before4
2 Replies

5. Shell Programming and Scripting

Split Filename from Absolute Path

Hello, I have the string "/a/b/c/ddd.txt" and i want to get only the filename, in this case "ddd.txt". I have as something known in the script the pattern "/a/b/c/", so I`ve tried something like: echo "/a/b/c/ddd.txt" | cut -d "/a/b/c/" -f2 but it doesn`t go, any help?. thanks, bye (2 Replies)
Discussion started by: rubber08
2 Replies

6. Shell Programming and Scripting

Howto Print File Path or Print the Filename

I'm trying to clean up my samba share and need to print the found file or print the path of the image it tried to searched for. So far I have this but can't seem to get the logic right. Can anyone help point me in the right direction? for FILE in `cat list`; do if ; then ... (1 Reply)
Discussion started by: overkill
1 Replies

7. Shell Programming and Scripting

print the filename

#!/bin/ksh for files in `ls *.gz` do gunzip -c $files | awk -v s=$files -F\" '{print s","$6}' done I have tried FILENAME parameter but it did not work please help. (1 Reply)
Discussion started by: harshakirans
1 Replies

8. Shell Programming and Scripting

Split and print

I have a file with data such as: X Y Z 4 1,3,5,7, 4,6,8,10, A B C 3 2,3,4, 5,9,11, E F G 5 1,2,3,4,5, 8,9,10,11,12, Columns 1, 2 and 3 are descriptions. Column 4 tells how many numbers are in columns 5 and 6 What I'd like to do is split column 5 and column 6 by the "," and then... (3 Replies)
Discussion started by: dcfargo
3 Replies

9. Shell Programming and Scripting

using grep and print filename

Hi, I have a question on bash. Basically I would like to print a file name using bash. I am actually trying to grep a particular character in sequential files. I have alot files such that a.txt, b.txt,c.txt...etc. If I found a certain character, I would print that particular filename. I... (5 Replies)
Discussion started by: ahjiefreak
5 Replies

10. UNIX for Dummies Questions & Answers

Use same filename for prefix in the split command

I want to execute something like this: find . -type f -regex '$REGEX' -print | xargs split -d -C $SIZE The problem is that I want the name of the split files to be the same name as original files. So if my directory has two files called abc.txt and def.txt, then I want the split files to be... (1 Reply)
Discussion started by: namityadav
1 Replies
Login or Register to Ask a Question