Solved

Replacing a string with fixed position - Linux

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ubuntu not booting - How get past GRUB? 3 40
AWS vpc peering connection 1 48
CentOS 7 Installation 7 72
how to configure linux OS using Ubuntu 7 35
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 …
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

813 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

16 Experts available now in Live!

Get 1:1 Help Now