awk Splitting strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Splitting strings
# 1  
Old 11-22-2013
awk Splitting strings

Hi All,

There is a file with a data. If the line is longer than 'n', we splitting the line on the parts and print them. Each of the parts is less than or equal 'n'.

For example:

n = 2;

"ABCDEFGHIJK" -> length 11

Results:
"AB" "CD" EF" GH" "IJ" "K"

Code, but there are some errors. What's the problem?

Code:
#!/bin/bash

echo "Max length of line: "
read n

awk '
    function pomnoz(a, b){
        return 1/(1/a / b);
    };
    
    { MAX = $n };
    {
        LEN = length(\$0);
        
        if(LEN % MAX != 0){
            N = (LEN - (LEN % MAX)) / MAX + 1;
        };
        
        if(LEN % MAX == 0){
            N = LEN / MAX;
        };
        
        for(x = 0; x < N; x++){
            POS = (pomnoz(x, MAX) + 1);
            STR = substr(\$0, POS, MAX);
            {print STR};
        }
    }
' data

Thank's
# 2  
Old 11-22-2013
[edit] Oh, I see. You cannot dump variables inside awk like that.

In awk, $ doesn't mean variable, it means column. If you set n=2, $n would mean "column 2".

To get a variable into awk, put it before the filenames like awk '{ ... }' var="$var" filename

You can also put it before with -v: awk -v var="$var" '{ ... }' filename
# 3  
Old 11-22-2013
Try :

Code:
$ echo "ABCDEFGHIJK" | awk '{for(i=1;i<=length($1);i+=2)printf substr($1,i,2) OFS;printf RS}'
AB CD EF GH IJ K

Code:
$ echo "ABCDEFGHIJK" | awk '{for(i=1;i<=length($1);i+=n)printf substr($1,i,n) OFS;printf RS}' n=2
AB CD EF GH IJ K 

$ echo "ABCDEFGHIJK" | awk '{for(i=1;i<=length($1);i+=n)printf substr($1,i,n) OFS;printf RS}' n=3
ABC DEF GHI JK 

$ echo "ABCDEFGHIJK" | awk '{for(i=1;i<=length($1);i+=n)printf substr($1,i,n) OFS;printf RS}' n=4
ABCD EFGH IJK

# 4  
Old 11-22-2013
Code:
awk: 8: unexpected character '\'
awk: 20: unexpected charakter '\'


Last edited by Scott; 11-22-2013 at 12:30 PM.. Reason: Code tags
# 5  
Old 11-22-2013
Try nawk. If that doesn't work, tell us what you changed.
# 6  
Old 11-22-2013
Quote:
Originally Posted by booyaka
Hi All,

There is a file with a data. If the line is longer than 'n', we splitting the line on the parts and print them. Each of the parts is less than or equal 'n'.

For example:

n = 2;

"ABCDEFGHIJK" -> length 11

Results:
"AB" "CD" EF" GH" "IJ" "K"

Code, but there are some errors. What's the problem?

Code:
#!/bin/bash

echo "Max length of line: "
read n

awk '
    function pomnoz(a, b){
        return 1/(1/a / b);
    };
    
    { MAX = $n };
    {
        LEN = length(\$0);
        
        if(LEN % MAX != 0){
            N = (LEN - (LEN % MAX)) / MAX + 1;
        };
        
        if(LEN % MAX == 0){
            N = LEN / MAX;
        };
        
        for(x = 0; x < N; x++){
            POS = (pomnoz(x, MAX) + 1);
            STR = substr(\$0, POS, MAX);
            {print STR};
        }
    }
' data

Thank's
what your function is doing ? why backslash ?
This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 11-22-2013
He is trying to get shell variables into awk. I showed him the correct way.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. UNIX for Dummies Questions & Answers

Splitting strings based on delimiter

i have a snippet from server log delimited by forward slash. /a/b/c/d/filename i need to cut until last delimiter. So desired output should look like: /a/b/c/d can you please help? Thanks in advance. (7 Replies)
Discussion started by: alpha_1
7 Replies

3. UNIX for Dummies Questions & Answers

Splitting strings

I have a file that has two columns. I first column is an identifier and the second is a column of strings. I want to split the characters in the second column into substrings of length 5. So if the first line of the file has a string of length 10, the output should have the identifier repeated 2... (3 Replies)
Discussion started by: verse123
3 Replies

4. Shell Programming and Scripting

splitting tab delimited strings

hi i have a requirement to input a string to a shell script and to split the string to multiple fields, the string is copied from a row of three columns (name,age,address) in an excel sheet. the three columns (from excel) are seperated with a tab when pasted in the command prompt, but when the ... (2 Replies)
Discussion started by: midhun19
2 Replies

5. Shell Programming and Scripting

Splitting Concatenated Words With Largest Strings First

hello, I had posted earlier help for a script for splitting concatenated words . The script was supposed to read words from a master file and split concatenated words in the slave/input file. Thanks to the help I got, the following script which works very well was posted. It detects residues by... (14 Replies)
Discussion started by: gimley
14 Replies

6. UNIX for Dummies Questions & Answers

awk, splitting date

can any1 explain me hw is below wrking: wat is substr and dd,mmyear used for wat values will go in dese? sdt='31122010235959' sdate=`validate_date $sdt` validate_date() { dt="$1" set `echo $dt | nawk '{ print... (2 Replies)
Discussion started by: musu
2 Replies

7. Programming

Splitting strings from file

Hi All I need help writing a Java program to split strings reading from a FILE and writing output into a FILE. e.g., My input is : International NNP Rockwell NNP Corp. NNP 's POS Tulsa NNP unit NN said VBDExpected output is: International I In Int Inte l al... (2 Replies)
Discussion started by: my_Perl
2 Replies

8. UNIX for Dummies Questions & Answers

Record splitting with AWK

Hi all ! I need your help as quick as possible. My input file like this: bạc těnh ( 薄情) 1 . 薄情な.2. 夫婦或いは男女の不貞を指す。 bách (百,迫)1.100ドソ. tr a m b a c ともいう. 2.柏(カヽしわ)・ 3.圧迫する.4.差し迫った, My propose is take the value in the firt bracket. I used the command like : ...if (index(... (6 Replies)
Discussion started by: maixu134
6 Replies

9. Shell Programming and Scripting

Splitting a string with awk

Hi all, I want to split a string in awk and treat each component seperatley. I know i can use: split ("hi all", a, " ") to put each delimited component into array a. However when i want to do this with just a string of chars it does not work split ("hi", a, ""); print a; prints... (6 Replies)
Discussion started by: pxy2d1
6 Replies

10. UNIX for Dummies Questions & Answers

splitting strings

Hi you, I have the following problem: I have a string like the followings: '166Mhz' or '128MB' or '300sec' or ... What I want to do is, I want to split the strings in a part with the numbers and a part with letters. Since the strings are not allway three digits and than text i couldn't do... (3 Replies)
Discussion started by: bensky
3 Replies
Login or Register to Ask a Question