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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to put a word starting at particular position in a file using shell scripting
# 8  
Old 11-27-2009
not working

Quote:
Originally Posted by radoulov
Something like this:

Code:
awk 'NR == FNR {
  /^[ \t]*\47name/ && c++      # get the field number
  if (/^[ \t]*\47size/) {
    split($0, t, ":")          
    gsub(/[ \t\47,]/, x, t[2]) # strip punctuation
    fmt[c] = t[2]              # get the size 
    }
  next                         # run the above actions                                     
  }                            # + only for the first input file
{ 
  for (i=1; i<=NF; i++)        # output the strings in the correct format
    printf "%" (length($i) > fmt[i] ? "." : "-" ) fmt[i] "s", $i           
  print x
  }' config.txt temp.txt


Needs some small modification i guess.
The input temp.txt
Code:
1       12      123     1234    1
A       AB      ABC     ABDC    e

config.txt
Code:
{
                        'name'  :       'Field1',
                        'type'  :       'input',
                        'spos'  :       1,
                        'size'  :       2,
                },
                {
                        'name'  :       'Field2',
                        'type'  :       'input',
                        'spos'  :       3,
                        'size'  :       2,
                },
                {
                        'name'  :       'Field3',
                        'type'  :       'input',
                        'spos'  :       5,
                        'size'  :       8,
                },
                {
                        'name'  :       'Field4',
                        'type'  :       'input',
                        'spos'  :       13,
                        'size'  :       11,
                },
                {
                        'name'  :       'Field5',
                        'type'  :       'input',
                        'spos'  :       24,
                        'size'  :       11,
                },

OUTPUT is as follows
Code:
1 12123     12341
A ABABC     ABDCe

Expected is
Code:
1 12123     1234       1
A ABABC     ABDC       e


Last edited by subhrap.das; 11-27-2009 at 07:36 AM.. Reason: improper code
# 9  
Old 11-27-2009
What version of AWK/OS you're using? I get the expected result (I added cat -e to show you the line endings):

Code:
% cat config.txt
{
                        'name'  :       'Field1',
                        'type'  :       'input',
                        'spos'  :       1,
                        'size'  :       2,
                },
                {
                        'name'  :       'Field2',
                        'type'  :       'input',
                        'spos'  :       3,
                        'size'  :       2,
                },
                {
                        'name'  :       'Field3',
                        'type'  :       'input',
                        'spos'  :       5,
                        'size'  :       8,
                },
                {
                        'name'  :       'Field4',
                        'type'  :       'input',
                        'spos'  :       13,
                        'size'  :       11,
                },
                {
                        'name'  :       'Field5',
                        'type'  :       'input',
                        'spos'  :       24,
                        'size'  :       11,
                },
% cat temp.txt 
1       12      123     1234    1
A       AB      ABC     ABDC    e
% [I]awk 'NR == FNR {
  /^[ \t]*\47name/ && c++      # get the field number
  if (/^[ \t]*\47size/) {
    split($0, t, ":")
    gsub(/[ \t\47,]/, x, t[2]) # strip punctuation
    fmt[c] = t[2]              # get the size
    }
  next                         # run the above actions
  }                            # + only for the first input file
{
  for (i=1; i<=NF; i++)        # output the strings in the correct format
    printf "%" (length($i) > fmt[i] ? "." : "-" ) fmt "s", $i
  print x
  }' config.txt temp.txt|cat -e
1 12123     1234       1          $
A ABABC     ABDC       e          $

# 10  
Old 11-27-2009
You are correct. Its working on AIX Version 5 (IBM machine)
but not on HP-UX.
Can i please have a machine independent code?

Edited by radoulov: Sorry, I edited your code by mistake, see my answer below.

Last edited by radoulov; 11-27-2009 at 08:23 AM.. Reason: additional input
# 11  
Old 11-27-2009
Quote:
Can i please have a machine independent code?
Try nawk instead of awk on HP-UX.


Quote:
Also if the temp.txt has a space then the code doesnt work.
Try to set explicitly the field separator:

Code:
awk -F'\t' ...

# 12  
Old 11-30-2009
Thanks Radalouv

Hi Radalouv,
Your query works in all scenarios but only in AIX.
nawk doesnt work in HP-UX as well.

Any help in this regard will be great.
Thanks for all your help.
# 13  
Old 11-30-2009
Cuold you please post (copy/paste) the real input data (only if different from what you already provided), the exact command that you' re executing on HP-UX and the output you' re getting?
# 14  
Old 11-30-2009
This is the real input and output i'm trying rt now.

no change whatever i had pasted earlier.

Example 1:
I/p is
Code:
1       12      123     12 34   1
A       AB      ABC     ABDC    e

OUTPUT in HP-UX is as follows 

1 12123     12341
A ABABC     ABDCe


Expected/IBM output is  

1 12123     1234       1
A ABABC     ABDC       e


Example 2:

I/p is
Code:
1               123     12 34   1
A       AB      ABC     ABDC    e
cat -t tab.txt
1^I^I123^I12 34^I1
A^IAB^IABC^IABDC^Ie

HP o/p:
1 1212      341
A ABABC     ABDCe

IBM/expected o/p
1               123     12 34   1
A       AB      ABC     ABDC    e


Last edited by subhrap.das; 11-30-2009 at 07:47 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Write a word at 72nd position of a matched line in a file

Hi, I need to search a file for a pattern,replace some other word and write a word at its 72nd position. For example, My name is Mano.Im learning Unix. I want to search the file in all lines containing the word "Mano".In that matched line,replace the word "Unix" with "Java".And... (5 Replies)
Discussion started by: mano1 n
5 Replies

2. Shell Programming and Scripting

Grep the 5th and 6th position character of a word in a file

I am trying to find/grep the 5th and 6th position character (TX) of a word in a file. I tried to do grep "....TX" file The output gives me everything in the file with TX in it. I only need the output with the TX in the 5th and 6th position of the word. Any idea Example: test1 car... (5 Replies)
Discussion started by: e_mikey_2000
5 Replies

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

4. Shell Programming and Scripting

removing a word in a multiple file starting at the dot extension

hi I would like to ask if someone knows a command or a script on how to rename a multiple file in the directory starting at the end of the filename or at the .extension( i would like to remove the last 11 character before the extension) for example Below is the result of my command ls inside... (5 Replies)
Discussion started by: jao_madn
5 Replies

5. Shell Programming and Scripting

Find the starting position in a file

I have a file called "INPUT" which takes the following format MNT-BANK-NUMBERO:006,00:N MNT-100-ACCOUNT-NUMBERO:018,00:N MNT-1000-DESCRIPTIONO:045:C . . . Now i got to find the displacements of the account numbers of each field of a file. For the field MNT-BANK-NUMBERO:006,00:N, the... (4 Replies)
Discussion started by: bobby1015
4 Replies

6. UNIX for Dummies Questions & Answers

Script to delete a word based on position in a file

Hi, I am new to unix. I want to delete 2 words placed at position say for example at 23rd and 45th position in a line. I used sed but couldnt achieve this. Example: the file contains 2 lines 12345 98765 "12345" 876 12345 98765 "64578" 876 I want to delete " placed at position 13 and 19... (4 Replies)
Discussion started by: nbks2u
4 Replies

7. Shell Programming and Scripting

Starting Shell Scripting

Hi, I am new to shell scripting ,infact today only i started working on it. Can any body tell me some Good tutorials that i can use and that are easy to understand I shall be obliged (1 Reply)
Discussion started by: Gousia
1 Replies

8. UNIX for Dummies Questions & Answers

Paste a word in the third field position of a file

Hi All, I have a file like this, 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 The above line is a comma separted data file. I want to modify the third field to The final data file should be like 0.0.0.1 /account 327706,Data Cleansing,,,CRM error,100,0 ... (1 Reply)
Discussion started by: girish.raos
1 Replies

9. Shell Programming and Scripting

search a word in a xml file and print the out put

hi , i m having a html file and this file looks like this <ssl> <name>PIA</name> <enabled>true</enabled> <listen-port>39370</listen-port> </ssl> <log> <name>PIA</name> </log> <execute-queue> <name>weblogic.kernel.Default</name> ... (7 Replies)
Discussion started by: becksram123
7 Replies

10. Shell Programming and Scripting

How to check a word position in a file ?

Hello everybody, I have a file like this : "window 1 truck 3 duck 2... fire 1... etc..." and I would like to print the following number of a word I am searching for. (For example here, if I search for the word "fire", I will print "1") Thank you for your help ! (7 Replies)
Discussion started by: tibo
7 Replies
Login or Register to Ask a Question