Solved

Replacing a string with fixed position - Linux

Posted on 2014-12-01
6
171 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
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

821 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