Visit Our UNIX and Linux User Community


AWK split


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK split
# 1  
Old 09-04-2009
Question AWK split

Dear colleagues! I want to create a script which will take each file from the list and then parse it filename with awk/split. I do it this way:


Code:
for file in `cat /$FileListFN`; do
     echo `awk   '
                 {N=split(FILENAME,FNParts,"_")}
                 {for (i=1; i<=N; i++)
                    {print FNParts[i] " | "}
                 }
                 ' $file`
done

I use this file list as a source :
Code:
BD52_New_28_04_09.xml
BD5A_New_28_04_09.xml
BD62_New_28_04_09.xml
BD6A_New_28_04_09.xml
BD72_New_28_04_09.xml
BD7A_New_28_04_09.xml
BD82_New_28_04_09.xml
BD8A_New_28_04_09.xml

But when I run it I get the following:
Code:
BD52 | New | 28 | 04 | 09.xml | BD52 | New | 28 | 04 | 09.xml | BD52 | New | 28 | 04 | 09.xml |
BD5A | New | 28 | 04 | 09.xml | BD5A | New | 28 | 04 | 09.xml | BD5A | New | 28 | 04 | 09.xml |
BD62 | New | 28 | 04 | 09.xml | BD62 | New | 28 | 04 | 09.xml | BD62 | New | 28 | 04 | 09.xml |
BD6A | New | 28 | 04 | 09.xml | BD6A | New | 28 | 04 | 09.xml | BD6A | New | 28 | 04 | 09.xml |
BD72 | New | 28 | 04 | 09.xml | BD72 | New | 28 | 04 | 09.xml | BD72 | New | 28 | 04 | 09.xml |
BD7A | New | 28 | 04 | 09.xml | BD7A | New | 28 | 04 | 09.xml | BD7A | New | 28 | 04 | 09.xml |
BD82 | New | 28 | 04 | 09.xml | BD82 | New | 28 | 04 | 09.xml | BD82 | New | 28 | 04 | 09.xml |
BD8A | New | 28 | 04 | 09.xml | BD8A | New | 28 | 04 | 09.xml | BD8A | New | 28 | 04 | 09.xml |

Why my strings repeat three times? I did everything as it was shown in examples. I try to do it on Solaris.

Thank you in advance!

Last edited by Franklin52; 09-04-2009 at 06:51 AM.. Reason: Please use code tags!
# 2  
Old 09-04-2009
What is the content of the variables $FileListFN and $file and what should be the desired output?

Regards
# 3  
Old 09-04-2009
$FileListFN contains the full name of list-file. $file is a current processing file. Idea is the following - I want to have two arrays, one with parsed filename (FNPart) and second with parsed filename template (Tag). Then I could produce the following string <Tag[1]><FNPart[1]></Tag[1]> ... <Tag[n]><FNPart[n]></Tag[n]> and then insert it into each XML between first "><" combination. So I don't want to output arrays, I want to produce the full string and then use it.

---------- Post updated at 03:08 PM ---------- Previous update was at 02:56 PM ----------

By the word, now I know what happened - each my XML has exactly three strings inside, so FILENAME was parsed three times...
# 4  
Old 09-04-2009
This should be sufficient:

Code:
awk '$1=$1' FS="_" OFS=" | " file

# 5  
Old 09-04-2009
If u just want to replace '_' with '|' can be done following ways..

Code:
cat filename|sed 's/_/|/g' OR sed 's/_/|/g' filename

# 6  
Old 09-07-2009
Thank you very much for your answers! Now I am trying to use a variable from main script in awk program. I passed this variable into my awk program, but it doesn't work correctly. The script is given below:
awk '{print $FNT}' $file -v FNT=$FileNameTemplate
file here is a real XML fliename, FileNameTemplate is something like JFJF_New_07_09_09. I expect this script to output the value of the variable, but it outputs my XML file and then waits for an input, doing nothing and repeating my input after Enter button pressed... Please tell me where is my mistake and what should I read before I will ask such questions.
# 7  
Old 09-07-2009
Not quite clear with what exactly you want but try this...

Code:
awk -v FNT=${FileNameTemplate} '{print FNT}' $file


Previous Thread | Next Thread
Test Your Knowledge in Computers #399
Difficulty: Medium
The 11i v2 release of HP-UX introduced kernel-based intrusion detection, strong random number generation, stack buffer overflow protection, security partitioning, role-based access management, and various open-source security tools.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk split and awk calculation in the same command

I am trying to run the awk below. My question is when I split the input, then run anotherawk to perform a calculation using that splitas the input there are no issues. When I try to combine them the output is not correct, is the split not working or did I do it wrong? Thank you :). input ... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

awk split help

Hello, I have the following input file: A=1;B=2;C=3;D=4 A=4;B=6;C=7;D=9 I wish to have the following output 1 2 3 4 4 6 7 9 Can awk split be used to do this? I have done this without using split, but the process is quite tedious. Any help is appreciated! (4 Replies)
Discussion started by: Rabu
4 Replies

3. Shell Programming and Scripting

awk split numbers

I would like to split a string of numbers "1-2,4-13,16,19-20,21-25,31-32" and output these with awk into -dFirstPage=1 -dLastPage=2 file.pdf -dFirstPage=4 -dLastPage=13 file.pdf -dFirstPage=16 -dLastPage=16 file.pdf file.pdf -dFirstPage=19 -dLastPage=20 file.pdf -dFirstPage=21 -dLastPage=25... (3 Replies)
Discussion started by: sdf
3 Replies

4. 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

5. UNIX for Dummies Questions & Answers

awk split

Hi Folks, I have lines that look like this: >m110730_101608_00120_c100168052554400000315046108261127_s1_p0/7/29_426ACGTGCTATGCGG >m110730_101608_00120_c100168052554400000315046108261127_s1_p0/7/469_894ACGTGCTATGCGG I want to split all lines into: ... (4 Replies)
Discussion started by: heecha
4 Replies

6. UNIX for Dummies Questions & Answers

awk split

Can anybody tell me what is wrong with this ? It does not produce anyoutput. awk 'split( "this:that", arr,":")' (2 Replies)
Discussion started by: jville
2 Replies

7. Shell Programming and Scripting

awk to split string

Hello Friends, Im trying to split a string. When i use first method of awk like below i have an error: method1 (I specified the FS as ":" so is this wrong?) servert1{root}>awk -f split.txt awk: syntax error near line 2 awk: bailing out near line 2 split.txt:... (5 Replies)
Discussion started by: EAGL€
5 Replies

8. Shell Programming and Scripting

split file with awk

I did a lot of search on this forum on spiting file; found a lot, but my requirement is a bit different, please guide. Master file: x:start:5 line1:23 line2:12 2:90 x:end:5 x:start:2 45:56 22:90 x:end:2 x:start:3 line1:23 line2:12 x:end:3 x:start:2 line5:23 (1 Reply)
Discussion started by: uwork72
1 Replies

9. Shell Programming and Scripting

awk - split function

Hi, I have some output in the form of: #output: abc123 def567 hij890 ghi324 the above is in one column, stored in the variable x ( and if you wana know about x... x=sprintf(tolower(substr(someArray,1,1)substr(userArray,3,1)substr(userArray,2,1))) when i simply print x (print x) I get... (7 Replies)
Discussion started by: fusionX
7 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

Featured Tech Videos