?
Solved

Replacing a string with fixed position - Linux

Posted on 2014-12-01
6
Medium Priority
?
177 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 1000 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 1000 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
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 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