Solved

Replacing a string with fixed position - Linux

Posted on 2014-12-01
6
165 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 61

Expert Comment

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

Accepted Solution

by:
Gerwin Jansen 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 37

Expert Comment

by:Gerwin Jansen
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now