How to use this position parameter?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use this position parameter?
# 1  
Old 06-06-2013
How to use this position parameter?

Hi Gurus,
I want split one huge line file to multiple line, I got code from you guys yesterday.

I want put the code in a script which use two position parameter when run the script.
my code like:

Code:
awk '{for (i=1; i<=length($0); i+=$2) print substr($0, i, $2)}' $1 > $1_split

when I run the script, I type as
Code:
scriptname file_name liine_length

for some reason, the script doesn't pick $2 value, if I hardcode value instead of $2, the script works.
Code:
awk '{for (i=1; i<=length($0); i+=5) print substr($0, i, 5)}' $1 > $1_split

Can anybody help me out this.

Thanks in advance.
# 2  
Old 06-06-2013
Assign it to an awk variable and use it:
Code:
awk -v L=$2 '{for (i=1; i<=length($0); i+=L) print substr($0, i, L)}' $1 > $1_split


Last edited by Yoda; 06-06-2013 at 01:36 PM.. Reason: correction
This User Gave Thanks to Yoda For This Post:
# 3  
Old 06-06-2013
maybe you have to do it like this
Code:
awk '{ print var}' var=$2

Yoda beat me by seconds Smilie
You can have variable in front using -v or after code like I do
This User Gave Thanks to Jotne For This Post:
# 4  
Old 06-06-2013
Quote:
Originally Posted by Jotne
maybe you have to do it like this
Code:
awk '{ print var}' var=$2

Yoda beat me by seconds Smilie
You can have variable in front using -v or after code like I do
Hi Jotne,
I tried the way you provided, the process was hanging up

Code:
#!/bin/sh
echo "First position para is $1"
echo "Second position para is $2"
awk '{print var}' var=$2

Code:
sh -x splitfile file 4

result:
Code:
+ echo 'First position para is file'
First position para is file
+ echo 'Second position para is 4'
Second position para is 4
+ awk '{print var}' var=4

would you please take a look.

Thank you very much

---------- Post updated at 03:17 PM ---------- Previous update was at 03:15 PM ----------

Quote:
Originally Posted by Yoda
Assign it to an awk variable and use it:
Code:
awk -v L=$2 '{for (i=1; i<=length($0); i+=L) print substr($0, i, L)}' $1 > $1_split

It works fine.
Thank you very much
Smilie
# 5  
Old 06-06-2013
awk needs input or a BEGIN block, it was just an example.
Example Yoda uses has $1 as input file.
# 6  
Old 06-06-2013
Quote:
Originally Posted by Jotne
awk needs input or a BEGIN block, it was just an example.
Example Yoda uses has $1 as input file.
when I run the script I have input position parameter. for example:

Code:
sh script para1 para2

the script like:
Code:
#!/bin/sh
echo "First position para is $1"
echo "Second position para is $2"
awk '{print var}' var=$2

Thanks
# 7  
Old 06-07-2013
Your awk does not hang up, it needs an input file and because it is missing, awk is waiting for input. Also it is best to quote those references to positional shell parameters..
Code:
awk '{print var}' var="$2" "$1"

or like Yoda suggests:
Code:
awk -v L="$2" ....  "$1"[/COLOR]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting value at a particular position without changing the position of other characters

Hi All, I wanted a sed/awk command to add a value/character on a particular position without disturbing the position of other characters. I have file a.txt OL 10031 Day Black Midi Good Value P01 P07 OL 10031 Day Black Short Good Value P01 P07 I want to get the output as... (2 Replies)
Discussion started by: rahulsk
2 Replies

2. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

3. Shell Programming and Scripting

How to pass position parameter into function.?

Hi Gurus, I have request which needs to pass position parameter to a function. I tried below simple code, it doesn't work. #!/bin/bash func_1(){ echo $1 } func_1 $ ./set_file abc $ do I need add some to get the position para first? thanks in advance. (3 Replies)
Discussion started by: ken6503
3 Replies

4. Shell Programming and Scripting

Need command or script to print all lines from 2nd position to last but one position

hi guys, i want command or script to display the content of file from 2nd position to last but one position of a file abcdefghdasdasdsd 123,345,678,345,323 434,656,656,656,656 678,878,878,989,545 4565656667,65656 i want to display the same above file without first and... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

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

6. Shell Programming and Scripting

Moving first position in a file to the last position

hi, I have a file which consists of some records: 2010_06_4010093_001_001|10|ABCDEFGH|9|4010093||0040400||31.12.2009|S|O|X||||20100602093851-31.12.2009|XXBBFC|EFG||||00001| 2010_06_4010162_001_001|11|ABCDEFGH|9|4010162||0040400||31.12.2009|S|O|X||||20100602093851-31.12.2009|XXBBFC|EFG||||00002|... (11 Replies)
Discussion started by: pparthiv
11 Replies

7. UNIX for Dummies Questions & Answers

find if a position is between a given start and end position

Hi, I am a newbie in unix programming so maybe this is a simple question. I would like to know how can I make a script that outputs only the values that are not between any given start and end positions Example file1: 2 30 40 80 82 100 file2: ID1 1 ID2 35 ID3 80 ID4 81 ID6... (9 Replies)
Discussion started by: fadista
9 Replies

8. Shell Programming and Scripting

how to find a position and print some string in the next and same position

I need a script for... how to find a position of column data and print some string in the next line and same position position should find based on *HEADER8* in text for ex: ord123 abs 123 987HEADER89 test234 ord124 abc 124 987HEADER88 test235 ... (1 Reply)
Discussion started by: naveenkcl
1 Replies

9. Shell Programming and Scripting

Position independent Parameter passing

Hi all, When parameters are passed to the shell script, they are dereferenced by their position. For example, I call myTest.sh and pass two parameters param1 and param2 as following: ./myTest.sh param1 param2 In the script, myTest.sh, I refer to first parameter (param1 ) as $1 and second... (1 Reply)
Discussion started by: sonaluphale
1 Replies

10. Shell Programming and Scripting

how do I make dynamic parameter names? Or get the value of a parameter evaluated twi

Say I write something like the following: var1=1 var2=2 for int in 1 2 do echo "\$var$int" done I want the output to be: 1 2 Instead I get something like: $var1 $var2 (2 Replies)
Discussion started by: Awanka
2 Replies
Login or Register to Ask a Question