position change


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting position change
# 1  
Old 09-15-2011
position change

Hi All,

input file:
Code:
echo "a|b|c|d|test|123|same" | awk 'BEGIN {FS=OFS="|"} {print $1,$2,$7,$4,$5,$6}'

Got Output :
Code:
a|b|same|d|test|123

This is the sample input file with less fields. But my original file contains more field values. How to replace last value to 3rd postion value. Because no idea how many field file contains.
# 2  
Old 09-15-2011
How about this:

Code:
echo "a|b|c|d|test|123|same" | awk 'BEGIN {FS=OFS="|"} { $3=$NF;NF--;print }'

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 09-15-2011
Code:
 
awk -F\| '{$2=$2"|"$NF;for(i=1;i<NF;i++){if(i==NF-1){printf("%s\n",$i)}else{printf("%s|",$i)}}}'

# 4  
Old 09-15-2011
Thanks Chubler.
It is working.

Code:
echo "a|b|c|d|test|123|same" | awk 'BEGIN {FS=OFS="|"} { $3=$NF;NF--;print }'

I want to the how it is working.
Above part, $3=$NF -> means last field replaced in 3rd field and printing other fields NF--
how it is avoiding last field.?
# 5  
Old 09-15-2011
Quote:
Originally Posted by Jairaj
How to replace last value to 3rd postion value. Because no idea how many field file contains.

"NF" is a special variable which gives the "total number of fields in the current input record".

example (FS is "|")

Code:
input	value of NF
a|b|c|d	4
a|b|c	3
a|b	2

So you can access the value of last field with $NF.
# 6  
Old 09-15-2011
Thanks Kamaraj.

Working fine.

I am very eager to know how it is working..
Code:
awk -F\| '{$2=$2"|"$NF;for(i=1;i<NF;i++){if(i==NF-1){printf("%s\n",$i)}else{printf("%s|",$i)}}}'

My understanding is , printing 2 fields and then last field after that using for loop it will take from 1 to last field. In this case it will print all the value correct ?

How awk is handing this situatiion?
# 7  
Old 09-15-2011
Code:
 
$2=$2"|"$NF; # replacing the $2 value with $2 + "|" + $NF (last column value)
For loop # to print all the values except last value ( condition is i < NF)

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

2. UNIX for Beginners Questions & Answers

Delete columns with a specific title XXX, where the position change in each file

Goodmorning, I know how to cut a string and a column, and how to find a word. I have a file with over 100 columns. All columns have a title in the first line. I have to delete all columns with the XXX title. I can't use cut -f because the position of XXX columns change in each file, and in... (14 Replies)
Discussion started by: echo manolis
14 Replies

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

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

Change text at a variable position

Hi there script guru's I have an input file /tmp/in.txt of which the data is seperated by a : (example of the data) test1:zz:2000:2000:zzz te:a:2000:3333:bbb testabs:x:2004:3000:cccc I would like to run a scrip (bash) changing the data after the second ":" example Test for the value of... (3 Replies)
Discussion started by: KobusE
3 Replies

6. UNIX for Dummies Questions & Answers

Change a character based on its position number

Hi I have a text file that I want to change some of the characters based on their position. My file contain multiple lines and characters should be counted continuously line by line. For example, I want to convert the 150th T to C. What can I do? Here is a portion of my file:... (10 Replies)
Discussion started by: a_bahreini
10 Replies

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

8. Shell Programming and Scripting

Change many columns position/order

Hi everyone, Please some help over here. (I´m using cygwing) I have files with 40 columns and 2000 lines in average. I´m trying to change the order position as follow. Original columns position:... (3 Replies)
Discussion started by: cgkmal
3 Replies

9. Shell Programming and Scripting

How to change a segment in a particular position

I need help in removing a leading zero in a particular position. For eg.: XYZ*04567472*0099*020091231*0123*0.12 In the above line, I want to replace "*0123" with "123" and "0.12" with ".12". I want to remove the leading zero only in position number 4 and 5 (the bolded segments) I was able... (10 Replies)
Discussion started by: ananthmm
10 Replies

10. Shell Programming and Scripting

Change Position of word character

Hi, I have following format in file aaa with content below, and would like to seek help from forumer about how to change and swap the position on 2nd field. 5874957|901125| 95874960|650614| 95874966|870308| 901125 to be changed as 25-11-1990 for eg Can someone help please ?? :) ... (6 Replies)
Discussion started by: cedrichiu
6 Replies
Login or Register to Ask a Question