need help to position words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help to position words
# 1  
Old 03-31-2010
need help to position words

Hi,

I would like to have script that can do below,

position words in the lines in a file, from below format

Code:
firstname, lastname B200000
.
.
.

into this format -

Code:
<5 spaces>first name, lastname<29 spaces from extreme left>B200000
.
.
.

Any idea how I can do this?
Smilie
# 2  
Old 03-31-2010
not sure what '29 spaces from the extreme left' means, but here's a start:
Code:
echo 'firstname,lastname B200000' | nawk -F, '{split($2,a, " "); printf("    %s,%-*s%s\n", $1, 29-length(a[2]),a[1],a[2])}'

# 3  
Old 03-31-2010
MySQL

thank you for quick reply to help me.

Code:
 	 not sure what '29 spaces from the extreme left' means, but here's a start:

29 spaces from left and not from the end character of last name.

Now coming to back to script, I will try what you have suggested but I also need to do it for other lines in the files so some loop will be required further I guess.Smilie

EDIT 1,

Code:
manual command is working fine, now we will need a loop to take care of all lines in the file, thank you!


EDIT 2,

I tried below,
Code:
while read LINE; do awk '{print $1 $2" "$3}'|awk -F, '{split($2,a, " "); printf("    %s,%-*s%s\n", $1, 29-length(a[2]),a[1],a[2])}'; done < hmm.txt

where hmm.txt contains the "firstname, lastname Number" on every line.

Problem with the output I get from above script is that the "Number" gets positioned at 29 spaces after last name instead I want it from 29 spaces from the beginning of the line
Any help?

Last edited by upengan78; 03-31-2010 at 05:58 PM..
# 4  
Old 03-31-2010
Code:
nawk -F, '{split($2,a, " "); printf("    %s,%-*s%s\n", $1, 29-length(a[2]),a[1],a[2])}' myFile

# 5  
Old 03-31-2010
Quote:
Originally Posted by vgersh99
Code:
nawk -F, '{split($2,a, " "); printf("    %s,%-*s%s\n", $1, 29-length(a[2]),a[1],a[2])}' myFile

Smilie this is cool. but again I want Number to be 29 places from the beginning of that line and not from 29 spaces from last name. Sorry, if I wasn't clear in the first place. Smilie
# 6  
Old 03-31-2010
Code:
nawk -F, '{split($2,a, " "); printf("    %s,%-*s%s\n", $1, 29-length($1)-6,a[1],a[2])}' myFile

# 7  
Old 03-31-2010
Quote:
Originally Posted by vgersh99
Code:
nawk -F, '{split($2,a, " "); printf("    %s,%-*s%s\n", $1, 29-length($1)-6,a[1],a[2])}' myFile

Awsome. thanks so much. This worked fine. Now I have issues with some lines where there exists a terribly long first or last name but I 'll take responsibility to take care of that Smilie

Thank you once again.

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

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

Put words to fix position in a file

Hi all, There are several lines in my file as a=123,b=dene,c=2312,d=234234,g=vxcvxcv,h=44 a=3,b=dene,c=22,d=23422342334,g=vxcvxcv,h=4 a=123,b=dene,c=2312,d=234234,g=vxcvxcv,h=678 I take values with this command awk -F '' '{print $1,$2,$3}' a.txt I want to put values to a fix position... (6 Replies)
Discussion started by: bahadiraktan
6 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

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

8. UNIX for Dummies Questions & Answers

To Extract words from File based on Position

Hi Guys, While I was writing one shell script , I just got struck at this point. I need to extract words from a file at some specified position and do some comparison operation and need to replace the extracted word with another word. Eg : I like Orange very much. I need to replace... (19 Replies)
Discussion started by: kuttu123
19 Replies

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

10. 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
Login or Register to Ask a Question