1851282 Members
4074 Online
104057 Solutions
New Discussion

Re: file maintenance

 
SOLVED
Go to solution
anthony dillard_1
Regular Advisor

file maintenance

i have a file that the contents looks like this

bl002amcvt187.federated.fds 0x00A0F8651EAF 2

i know how to delete (.federated.fds) using sed, but how do i delete the ending 2 when this value changes, any help would be nice
21 REPLIES 21
Peter Nikitka
Honored Contributor

Re: file maintenance

Hi,

I assume, that by
>>
the ending 2 when this value changes
<<
you mean:
with a field delimiter 'space' the last field containing numbers only

Try
sed -e 's/[.]federated[.]fds / /' -e 's/ [0-9][0-9]*$//'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: file maintenance

Hi Anthony:

# perl -anle 'print join " ", @F[0..(scalar @F-3)]' file

...will delete the last TWO fields (zero-relative) of any number of fields (as separated by whitespace) in each line of a file.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: file maintenance

Hi (again) Anthony:

Well, a bit more *obvious* is this way:

perl -anle 'print join " ", @F[0..($#F-2)]' file

# ...will delete the last TWO fields (zero-relative) of any number of fields (as separated by whitespace) in each line of a file.

Regards!

...JRF...
anthony dillard_1
Regular Advisor

Re: file maintenance

thanks folks but this is what i have

sed 's/.federated.fds//g' /home/b009021/host2mac1,
what ending do i need to used to delete the ending value in this case a number 0-9 for this entry in this file ie

before using sed
bl004amcvt031.federated.fds 0x00A0F83CD702 2

after sed
bl004amcvt031 0x00A0F83CD702 2
I still need to delete the 2

any ideas

Ninad_1
Honored Contributor

Re: file maintenance

You can use Peter's suggestion with a bit of modification

sed -e 's/[.]federated[.]fds / /' -e 's/ [0-9]$//'

Regards,
Ninad
anthony dillard_1
Regular Advisor

Re: file maintenance

tried, but didnt remove dittelly
A. Clay Stephenson
Acclaimed Contributor

Re: file maintenance

I think there must be some whitespace after the last digit. Try this sed:

sed -e 's/\.federated\.fds //' -e 's/ [0-9] *$//' < infile > outfile

This will match a single digit separated from the preceding data by a space and followed by zero or more spaces at the end of the line.
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: file maintenance

>Clay: sed -e 's/\.federated\.fds //' -e 's/ [0-9] *$//' < infile > outfile

This will delete the single digit on ALL lines.
Is this what Anthony wants?
James R. Ferguson
Acclaimed Contributor

Re: file maintenance

Hi:

Along the lines of Clay's thinking, perhaps *whitespace* (spaces and/or tabs) trails the last digit:

# sed -e 's/\.federated\.fds / /' -e 's/[0-9][ ]*$//' file > file.out

Note that the forum doesn't render spaces and/or tab characters nor the number thereof. Within the seemingly empty "[ ]" I typed a *space* and hit the *tab* key.

I'm sorry for not reading your original requirement better. I mistook "...delete the ending 2..." to mean "delete the ending 2 FIELDS...".

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: file maintenance

Hi,

additionally or instead of a space/TAB at the end of the line (after the ' 2') it may be, that there is a CR+LF at the end of the line (being a DOS borne file).
If you attach - perhaps only a part - of your input data we can verify this.

Extending my assumption about 'the ending' to:
"a field delimiter may be a TAB or space" I tend to some awk like this:

awk '/.federated.fds/ {sub("[.]federated[.]fds",""); printf $1;
for(i=2;i
mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
A. Clay Stephenson
Acclaimed Contributor

Re: file maintenance

Okay, to answer Dennis's concern (and frankly I don't know what the output should be), here's an all sed approach that should work (assuming there are zero or more trialing spaces).

sed -e 's/\(.*\)\(\.federated\.fds \)\(.*\)\( [0-9] *$\)/\1 \3/' < infile > outfile

It's using RE grouping \(...\) to grab everything before ".federated.fds " into group 1, and everything before the last space separated digit into group 3 and then substituting the saved group 1, a space, and group 3 for the line. Note that there are 4 RE Groups but we only want 2 of them when the entire RE is matched otherwise the line goes through unchanged.

If you want to filter out possible CR's in the input then the simplest way would probably be to use tr to feed this thing.


tr -d "\r" < infile | sed -e 's/\(.*\)\(\.federated\.fds \)\(.*\)\( [0-9] *$\)/\1 \3/' > outfile

Now tell me that ain't some ugly sed but it should work. I've carefully looked over the logic/syntax but not executed it.
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: file maintenance

>Clay: If you want to filter out possible CRs

do2ux(1) is made to do just that.
Dennis Handly
Acclaimed Contributor

Re: file maintenance

Oops:
>Clay: If you want to filter out possible CRs

dos2ux(1) is made to do just that.
James R. Ferguson
Acclaimed Contributor

Re: file maintenance

Hi (again):

At this point it would be most helpful if you posted representative lines from the file. In fact, posting :

# xd -tcx file

...output would settle the questions of whitespace and line endings (whitespace, LF, CRLF).

Are you committed to 'sed'? The syntax becomes clumsy and it's regular expressions are not as powerful nor easily deployed as Perl's as/if data becomes more variable and complex.

Regards!

...JRF...
anthony dillard_1
Regular Advisor

Re: file maintenance

sorry about the non response to all of your help, I just had to take a weekend for myself, I will try all of your suggestions and get back to you, again thanks for all of your input

Anthony
anthony dillard_1
Regular Advisor

Re: file maintenance

0007f70 8 \t 0 x 0 0 0 B 6 C 1 6 8 7 A 3
38093078 30303042 36433136 38374133
0007f80 \t 2 \n m c 2 1 3 a m c v t 1 0 8
9320a6d 63323133 616d6376 74313038
0007f90 \t 0 x 0 0 1 0 2 0 0 0 3 B 9 1 \t
9307830 30313032 30303033 42393109
0007fa0 2 \n m c 2 1 3 a m c v t 1 0 9 \t
320a6d63 32313361 6d637674 31303909
here you go, thanks

Sandman!
Honored Contributor
Solution

Re: file maintenance

Imho you could cat(1) the file to see the invisible characters embedded in the data before deciding on the command:

# cat -vet filename
A. Clay Stephenson
Acclaimed Contributor

Re: file maintenance

Ok, what you thought were spaces are actually tabs; You could use awk -F "\t" to separate the fields but since I went to all the trouble to write that incredibly ugly sed, I think I will stick to it after converting your to spaces via tr but Perl would probably be the best since it has a regular expression metacharacter that can represent whitespace.

tr "\t" " " < infile | sed -e 's/\(.*\)\(\.federated\.fds \)\(.*\)\( [0-9] *$\)/\1 \3/' > outfile
If it ain't broke, I can fix that.
anthony dillard_1
Regular Advisor

Re: file maintenance

found my soultion

sed 's/.federated.fds//g' /home/b009021/host2mac1 | awk '{print $1" "$2}' > /ho
me/b009021/host2mac2
anthony dillard_1
Regular Advisor

Re: file maintenance

thanks for all your help
anthony dillard_1
Regular Advisor

Re: file maintenance

sed 's/.federated.fds//g' /home/b009021/host2mac1 | awk '{print $1" "$2}' > /ho
me/b009021/host2mac2