Solved

Replacing a string with fixed position - Linux

Posted on 2014-12-01
6
173 Views
Last Modified: 2014-12-02
Hi experts, I have an issue with a file called "test" with lines:
PSM_3L_ARO__INT!AT!PM!TR!1417090645!-10800!01/12/14 01:01:02.699 ART!1!!!W004!34!0
PSM_3L_ARO__INT!AT!PM!TR!1417090644!-10800!01/12/14 01:01:01.699 ART!1!!!W004!35!0

Open in new window

I want to replace the last columns (# 13) with nothing, then I need see:
PSM_3L_ARO__INT!AT!PM!TR!1417090645!-10800!01/12/14 01:01:02.699 ART!1!!!W004!34
PSM_3L_ARO__INT!AT!PM!TR!1417090644!-10800!01/12/14 01:01:01.699 ART!1!!!W004!35

Open in new window

I tried in a loop with many files like "test" in a list:
    for f in $(cat $TEMP_GIS/lista_$dt.lst)
             do
             awk -F "!" '{print $1 "!" $2 "!" $3 "!" $4 "!" $5 "!" $6 "!" $7 "!" $8 "!" $9 "!" $10 "!" $11 "!" $12}'  $f
          done

Open in new window

It works fine but doesn't write the files, only show me the output at screen.
Supposed redirecting the output:
 awk -F "!" '{print $1 "!" $2 "!" $3 "!" $4 "!" $5 "!" $6 "!" $7 "!" $8 "!" $9 "!" $10 "!" $11 "!" $12}'  $f > $f

Open in new window

but generates files "testX", empty
Could you help me? , some other way to do this?
Thankyou, Regards
0
Comment
Question by:carlino70
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 62

Expert Comment

by:gheist
ID: 40474266
You need to push output to different file.
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 250 total points
ID: 40474886
Is that last field always 1 character? If so then:

sed -i 's/!.$//' test

will remove that last field from the file 'test'.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 250 total points
ID: 40474932
Much easier to use sed, ie:

for f in $(cat $TEMP_GIS/lista_$dt.lst)
do
   sed -i  's#![0-9]*$##' $f
done

Open in new window

0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40475289
>> only show me the output at screen
Some basic redirection tips for you:

Use > to redirect output, to a new file for example.

So:

awk -F "!" '{print $1 "!" $2 "!" $3 "!" $4 "!" $5 "!" $6 "!" $7 "!" $8 "!" $9 "!" $10 "!" $11 "!" $12}'  input_file > new_file

after that you have a new_file and an existing file, you may rename afterwards:

mv new_file input_file

that way you changes are in the file with the original name (input_file)
0
 

Author Closing Comment

by:carlino70
ID: 40475694
Thanks, both solutions work!
0
 

Author Comment

by:carlino70
ID: 40475697
Thanks Gerwin Jansen, I appreciate your contribution

Regards
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question