awk Index to get position matches pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Index to get position matches pattern
# 1  
Old 12-01-2017
awk Index to get position matches pattern

Input data as below (filetest.txt):
Code:
1|22 JAN Minimum Bal 20.00 | SAT
2|09 FEB Extract bal 168.00BR | REM
3|MIN BAL | LEX

Output should be:
( If there is Date & Month in 2nd field of Input file, It should be seperated else blank. If There is Decimal OR Decimal & Currency in last of the 2nd field of Input file, It should be seperated Else blank )

Code:
1|22 JAN | Minimum Bal | 20.00 | SAT
2|09 FEB | Extract bal | 168.00BR | REM
3||MIN BAL || LEX

My Code below. i feel index function wrong, Any help plz..
Code:
awk '{if ($2 ~ /^[0-9][0-9] [A-Z][A-Z][A-Z]/) {print $1"|"substr($2, 1, 7)"|"substr($2, 7, 40)} else  print $1"|" "|" $2  }' FS="|" filetest.txt | awk '{if ($3 ~ /[0-9][0-9].*?$/) {s=index($3,/[0-9][0-9].*?$/); print $1"|"$2"|"substr($3, 1, s-1)"|"substr($3, s+1, 20)} else  print $1"|"$2"|"$3"|"  }' FS="|"


Last edited by Don Cragun; 12-01-2017 at 03:21 AM.. Reason: Change HTML tags to CODE tags.
# 2  
Old 12-01-2017
Do you really want to truncate the contents of the newly created 3rd field if input field 2 contains more than 47 characters?

Why is the 2nd awk script in your pipeline looking for the currency values in field 3 when the currency values, if present, will be in field 4 by then?

Please explain exactly what the format is for the "decimal" and "decimal & Currency" strings. Will that field always be empty if a monetary amount is not present? Do there really have to be at least two digits in a monetary amount? Should a decimal point be required in a monetary amount? If a decimal point is present in a monetary amount, should there be a required number of decimal digits after that decimal point? Are there minimum and maximum numbers of uppercase letters in a currency code?
# 3  
Old 12-01-2017
Do you really want to truncate the contents of the newly created 3rd field if input field 2 contains more than 47 characters?
- i dont want to truncate. i didn't get logic so i mentioned values explicitely.

Please explain exactly what the format is for the "decimal" and "decimal & Currency" strings. Will that field always be empty if a monetary amount is not present?
- "decimal" have two decimal points. "decimal & Currency" have two decimal points and two uppercase alphabets.
# 4  
Old 12-01-2017
Would
Code:
awk -F\| '
        {sub (/^[0-9][0-9] [A-Z][A-Z][A-Z]/, "&|", $2)
         sub (/[0-9.]*[0-9][A-Z]* *$/, "|&", $2)
        }
1
' OFS="|" file
1|22 JAN| Minimum Bal |20.00 | SAT
2|09 FEB| Extract bal |168.00BR | REM
3|MIN BAL | LEX

come close to what you need?
# 5  
Old 12-01-2017
Quote:
Originally Posted by JSKOBS
Do you really want to truncate the contents of the newly created 3rd field if input field 2 contains more than 47 characters?
- i dont want to truncate. i didn't get logic so i mentioned values explicitely.

Please explain exactly what the format is for the "decimal" and "decimal & Currency" strings. Will that field always be empty if a monetary amount is not present?
- "decimal" have two decimal points. "decimal & Currency" have two decimal points and two uppercase alphabets.
Your sample data also has a leading <space> and a trailing <space> around the monetary value. Are they supposed to be there or not?
# 6  
Old 12-01-2017
records of kind 3rd row should be below: (Blank if No date & Month; Blank if no decimal at the end of 2nd field of input file)
Code:
3||MIN BAL|| LEX

---------- Post updated at 06:49 AM ---------- Previous update was at 05:57 AM ----------

Hi Don, Leading and trailing spaces are not necessary..
# 7  
Old 12-01-2017
How about
Code:
awk -F\| '
        {if (!sub (/^[0-9][0-9] [A-Z][A-Z][A-Z]/, "&|", $2)) $2 = FS $2
         if (!sub (/[0-9.]*[0-9][A-Z]* *$/, "|&", $2)) $2 = $2 FS
        }
1
' OFS="|" file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

find pattern matches in consecutive lines in certain fields-awk

I have a text file with many thousands of lines, a small sample of which looks like this: InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0 PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ... (5 Replies)
Discussion started by: jvoot
5 Replies

2. Shell Programming and Scripting

Number of matches and matched pattern(s) in awk

input: !@#$%2QW5QWERTAB$%^&* The string above is not separated (or FS=""). For clarity sake one could re-write the string by including a "|" as FS as follow: !|@|#|$|%|2QW|5QWERT|A|B|$|%|^|&|* Here, I am only interested in patterns (their numbers are variable between records) containing... (16 Replies)
Discussion started by: beca123456
16 Replies

3. Shell Programming and Scripting

How to get index values for multiple matches in the same line with awk?

Hi, I know that echo "bob alice robert alice" | awk '{print index($0,"alice")}' 5Will output the index of the first alice match, is there any way to get the index of all matches?, eg: echo "bob alice robert alice" | awk 'unknown magic' 5:18Thanks for your time. (6 Replies)
Discussion started by: chilicuil
6 Replies

4. Shell Programming and Scripting

awk with range but matches pattern

To match range, the command is: awk '/BEGIN/,/END/' but what I want is the range is printed only if there is additional pattern that matches in the range itself? maybe like this: awk '/BEGIN/,/END/ if only in that range there is /pattern/' Thanks (8 Replies)
Discussion started by: zorrox
8 Replies

5. Shell Programming and Scripting

awk pattern search with inconsistent position

Hi, Anybody knows how to get the value after the regexp and test it on pattern? The if the pattern matches, it will print the entire line on a separate file. Here's my raw file: ^_Name^_string^_Apple ^_Color^_string^_Red ^_Code^_string^_121 ^_Name^_string^_Banana ^_Code^_string^_123 ... (7 Replies)
Discussion started by: Jin_
7 Replies

6. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

7. Shell Programming and Scripting

awk to sum specific field when pattern matches

Trying to sum field #6 when field #2 matches string as follows: Input data: 2010-09-18-20.24.44.206117 UOWEXEC db2bp DB2XYZ hostname 1 2010-09-18-20.24.44.206117 UOWWAIT db2bp DB2XYZ hostname ... (3 Replies)
Discussion started by: ux4me
3 Replies

8. Shell Programming and Scripting

awk to count pattern matches

i have an awk statement which i am using to count the number of occurences of the number ,5, in the file: awk '/,5,/ {count++}' TRY.txt | awk 'END { printf(" Total parts: %d",count)}' i know there is a total of 10 matches..what is wrong here? thanks (16 Replies)
Discussion started by: npatwardhan
16 Replies

9. Shell Programming and Scripting

How to awk/sed/grep lines which contains a pattern at a given position

Dear friends I am new to linux and was trying to split some files userwise in our linux server. I have a data file of 156 continuous columns named ecscr final. I want the script to redirect all the lines containing a pattern of 7 digits to separate files. I was using grep to do that,... (2 Replies)
Discussion started by: anoopvraj
2 Replies

10. UNIX for Dummies Questions & Answers

How to select lines in unix matches a pattern at a particular position

I have huge file. I want to copy the lines which have first character as 2 or 7, and also which has fist two characters as 90. I need only these records from file. How I can acheive this. Can somebody help me..... (2 Replies)
Discussion started by: cs_banda
2 Replies
Login or Register to Ask a Question