Lines with strange characters and sed...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Lines with strange characters and sed...
# 1  
Old 10-28-2008
Question Lines with strange characters and sed...

Dear All:

I Have a bunch of files which I'd like to process with a shell script. The problem is that the files have strange characters in their headers, like

�g�8@L-000-MSG2__-ABCD________-FIRA_____-000001___-200806181330-__
��e�
Data from BLABLABLA, Instrument: BLABLA, Date: 2008/06/18 13:30Z
Row: 1078 Col: 1130 Lat: -22.267 Lon: 22.256 *** Something here ***

For my intents, I only need the information (in this case) from line 3 onwards. Sometimes this strange header occupies 2 lines, others 3...others...I don't know.

I made a very simple test, like

FILE=`find . -type f -name "FILENAME"`

for i in $FILE
do

FNOW=`echo $i`

#Cuts two first lines of the file
sed '1,2d' $FNOW > newfile
sed '/^$/d' -i newfile

HEADER=`head -1 newfile | cut -c1-4`
if [ "$HEADER" != "Data" ]
then
sed '1d' -i newfile
sed '/^$/d' -i newfile
fi

#A simple testing
HEADER2=`head -1 newfile | cut -c1-4`
echo ${HEADER2},${HEADER} >> test.txt

done



The problem is that.....sometimes i don't get to cut all the "strange" headers to obtain "clean" files, as you can see in some lines of test.txt

Data,@H
Data,ۘ
Data,Data
Data,@H

(etc)

So:
Is there any way to fulfill my intentions with sed? Maybe some "delete all the first lines until find the expression «Data»? Honestly, i don't know what else to try.

Thank you very much in advance
# 2  
Old 10-28-2008
I haven't tried this, but it may get you a step closer to what you want.
Code:
for f in $(find . -name SOMETHING*); do
    g="modified-$f"
    cp -iv $f $g
    while head -n1 $g|egrep -qv '^Data[[:print:]]{20}' && test -s $g; do
        sed 1d -i $g
    done
done

It loops through all the files found, makes a copy, and chops off the first line as long as it doesn't start with 'Data' followed by 20 printable characters.

Note that egrep -qv does not print any output (-q) and returns true if it does not (-v) find the regex. And test -s returns true if the file is greater than zero size.
# 3  
Old 10-31-2008
KenJackson:

Thank You very much for your suggestion.
With some modifications, your idea worked perfectly for me. Smilie

Best Regards,
Luis
# 4  
Old 10-31-2008
Hammer & Screwdriver There are some special character classes

Sometimes hard to test a file with strange characters without the file, but consider the following:

You may have seen where you can
Code:
echo "hello" | tr [:lower:] [:upper:]
HELLO

but there are others
[:alnum:] for printable characters
[:cntrl:] for control characters

Perhaps using one of the above might allow you to strip off the bad, or only carry forward the good characters.
# 5  
Old 10-31-2008
and maybe investigate the strings utility, which should at least help winnow out a lot of the crud first.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Strange characters in FORTRAN code output

Hi guys, After compiling a .f90 code and executing it, i get strange characters in the output file like : ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ Are these windows characters? how can i get rid of this? Much appreciated. Paul (1 Reply)
Discussion started by: Paul Moghadam
1 Replies

2. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

3. Hardware

Strange Characters from ILOM

Hello, I have an x86 server with an ILOM connection that produces strange characters when I perform a start /SP/console, see below: Oracle(R) Integrated Lights Out Manager Version 3.0.16.10.a r68533 Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. -> start... (9 Replies)
Discussion started by: kerrygold
9 Replies

4. Red Hat

Spanish Characters get converted in strange chrac

I am trying to sftp a textfile from windows to linux. The file includes some spanish characters. When I vi the file in LINUX, the special (spanish) characters get converted into some strange characters. anyone know how i can resolve this? for example México gets converted into México on LINUX. (0 Replies)
Discussion started by: mrx1350
0 Replies

5. Shell Programming and Scripting

Grep a file that may contain strange characters

Hello unix users :) I am trying to grep a string from a file that both the file and the string may have characters in them that are quite... strange, like würzburger. Well, bash reads this as W%C3%BCrzburger For example, if i do wget W%C3%BCrzburger the output is: --2012-01-08... (2 Replies)
Discussion started by: hakermania
2 Replies

6. Shell Programming and Scripting

awk filelist containing strange characters

I've written a script: find -depth | awk ‘ { if ( substr($1,length($0)-2,3) == “/1.” ) { print $1 } { system(“awk -f test1.awk “ $1 ) } } ‘ The idea is that it trundles through a large directory structure looking for files which are named '1.' and then... (3 Replies)
Discussion started by: nashcom
3 Replies

7. Shell Programming and Scripting

Sed - merge lines bw 2 specific characters

Hi, I have a bash script and I am looking for a command that will merge specific lines together. Sample Data: registration time = 1300890272 Id = 1 setd = 0 tagunt = 26 tagId=6, length=8, value= tagId=9, length=5, value= tagId=7, length=2, value= tagId=16, length=2, value= tagId=32,... (8 Replies)
Discussion started by: Winsarc
8 Replies

8. Shell Programming and Scripting

Strange Characters After Using Notepad

Hello all, I'm new to UNIX and new to this forum, so forgive my lack of knowledge. I'm new with editing in vi so I FTP scripts to a Windows machine and edit the script in notepad (when I need to do something quickly). Sometimes when I FTP the script back to the UNIX box, strange characters... (4 Replies)
Discussion started by: dgower2
4 Replies

9. Shell Programming and Scripting

Split a huge line into multiple 120 characters lines with sed?

Hello , I'm trying to split a file which contains a single very long line. My aim is to split this single line each 120 characters. I tried with the sed command : `cat ${MYPATH}/${FILE}|sed -e :a -e 's/^.\{1,120\}$/&\n/;ta' >{MYPATH}/${DEST}` but when I wc -l the destination file it is... (2 Replies)
Discussion started by: jerome_1664
2 Replies

10. UNIX for Dummies Questions & Answers

Strange Characters in Filename

Hi folks. None of the conventional methods are working for my dilemma: I have a file in my root directory that has a name comprised of strange characters. When I do an ls, it just hangs at that file until I do a Cntrl-C. rm ./filename & rm \filename do not work. I am entering the... (4 Replies)
Discussion started by: kristy
4 Replies
Login or Register to Ask a Question