Put words to fix position in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Put words to fix position in a file
# 1  
Old 11-16-2012
Put words to fix position in a file

Hi all,

There are several lines in my file as
Code:
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

Code:
awk -F '[=,]' '{print $1,$2,$3}' a.txt

I want to put values to a fix position (starting at same point) of a file as:
Code:
123         dene          2312        234234               44
3            dene          22            23422342334       4


$1 must start at 0th position of line
$2 must start at 19th position of line
$3 must start at 39th position of line
$4 must start at 50th position of line

Please help me to do this.

Thanks.

Last edited by Scrutinizer; 11-16-2012 at 04:29 PM.. Reason: code tags
# 2  
Old 11-16-2012
I'm confused. You awk program prints 3 values (and not the first three of the five you say are being output) and you give output column positions for 4 values.

Please give us a clear example explaining which fields you want selected from your sample input lines and show us what the output should actually look like. Please use code tags when you post your input file contents, code segments, and expected output examples.
# 3  
Old 11-16-2012
Is this what you are trying to achieve:-
Code:
awk -F '[=,]' '{printf "%-3s%19s%20s%18s\n", $2, $4, $6, $8}' a.txt
123               dene                2312            234234
3                 dene                  22       23422342334
123               dene                2312            234234

# 4  
Old 11-16-2012
Just as an fyi there is a great tool(csvfix - CSVfix is a tool for manipulating CSV data - Google Project Hosting) for dealing with csv, fixed format, etc.
For example, You could probably use a 'template' to accomplish what you want.
# 5  
Old 11-18-2012
As Don Cragun says, you leave us to wild guessing. Nevertheless, bipinajith's proposal might do what you want if slightly adapted:
Code:
awk -F'[=,]' '{printf "%-18s%-20s%-11s%-11s%-11s\n", $2,$4,$6,$8,$10}' file
123               dene                2312       234234     vxcvxcv    
3                 dene                22         23422342334vxcvxcv    
123               dene                2312       234234     vxcvxcv

# 6  
Old 11-18-2012
Quote:
Originally Posted by RudiC
As Don Cragun says, you leave us to wild guessing. Nevertheless, bipinajith's proposal might do what you want if slightly adapted:
Code:
awk -F'[=,]' '{printf "%-18s%-20s%-11s%-11s%-11s\n", $2,$4,$6,$8,$10}' file
123               dene                2312       234234     vxcvxcv    
3                 dene                22         23422342334vxcvxcv    
123               dene                2312       234234     vxcvxcv

After seeing bipinajith's proposal, I had considered this too. Note that in the 1st posting, the last output field comes from $12, not $10. I had come up with:
Code:
awk -F "[=,]" '{printf("%-19s%-20s%-11s%-20s%s\n",$2,$4,$6,$8,$12)}' file

which produces:
Code:
123                dene                2312       234234              44
3                  dene                22         23422342334         4
123                dene                2312       234234              678

but was waiting for bahadiraktan to respond to determine what value should be used instead of the 20 I have shown in red above. I had 19 for the first field's width instead of the 18 you used because bahadiraktan was specifying the 1st output column as #0 instead of the usual #1.
# 7  
Old 11-18-2012
Quote:
Originally Posted by Don Cragun
. . . . I had 19 for the first field's width instead of the 18 you used because bahadiraktan was specifying the 1st output column as #0 instead of the usual #1.
Good point - the #0 should have triggered my attention as well - but the entire request was, say, a bit inconsistent.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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. Shell Programming and Scripting

How to put delimiters in text files after fix characters?

Hi , i have a text file in which i want to put delimiters after certain characters ( fix),. like put a delimiter (any like ,) after 1-3 character than 4 than 5 than 6-17 ..... files looks like this (original)... (8 Replies)
Discussion started by: anamdev
8 Replies

3. Shell Programming and Scripting

How to concatene files and put each line of files on a specific position ?

Hi, I have some files that i want to concatene and put each of lines of this files on a specific position : File1 AAAAAAA BBBBBBB CCCCCCC File2 DDDDDDD EEEEEEE FFFFFFF File3 GGGGGG HHHHHH IIIIII New file (6 Replies)
Discussion started by: apippo70
6 Replies

4. Shell Programming and Scripting

Put numbers against the words

Hi All, I tried to solve this but the result gives me all zeros for one file. I failed to do for all 500 files. I have some 500 files with the extension .dat I have another set of files; 500 in number with extension .dic I created these .dic files by using sort -u from the actual .dat files.... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

5. Shell Programming and Scripting

Splitting Concatenated Words in Input File with Words from a Master File

Hello, I have a complex problem. I have a file in which words have been joined together: Theboy ranslowly I want to be able to correctly split the words using a lookup file in which all the words occur: the boy ran slowly slow put child ly The lookup file which is meant for look up... (21 Replies)
Discussion started by: gimley
21 Replies

6. Programming

how to put element of an array to first position.

hi, I have a array like my $array = ( "apple","ball","cat","dog","elephant"); how to push some element in the array to the first position. for example my final array should be elephant apple ball cat dog (5 Replies)
Discussion started by: vprasads
5 Replies

7. Shell Programming and Scripting

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 firstname, lastname B200000 . . . into this format - <5 spaces>first name, lastname<29 spaces from extreme left>B200000 . . . Any idea how I can do this? :b: (8 Replies)
Discussion started by: upengan78
8 Replies

8. Shell Programming and Scripting

How to put a word starting at particular position in a file using shell scripting

Hi all, I'm new to shell scripting and hence this query. I have 2 files. temp.txt and config.txt. The values in temp.txt are tab separated. ex: temp.txt AB CDE GHIJ OPQRS WXY ex:config.txt (1st line for 1st element of temp.txt and so on) start = '1' end='5' start = '6' end =... (26 Replies)
Discussion started by: subhrap.das
26 Replies

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