Scan and change file data content problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scan and change file data content problem
# 8  
Old 10-15-2010
Sorry for confusing you Smilie
As long as the letter (less or equal to 3 letter ) before the first "X" and after the last "X" is not "X".
I will replace those letter with "X"
# 9  
Old 10-15-2010
MySQL

Code:
# cat infile
XXXXXXXXXXSDFXXXXXDS
TREXXXXXXXSDFXXXXXDS

Code:
# ./justdoit infile
XXXXXXXXXXSDFXXXXXXX
XXXXXXXXXXSDFXXXXXXX

Code:
## justdoit ##
#!/bin/bash
rm -f tmpfileX
 while read -r l ; do
x=( $(echo $( echo $l |fold -w1 )) )
xr=( $(echo $( echo $l |fold -w1 )|rev) )
c=0
 for i in ${x[@]} ; do
  if [ "$i" != "X" ] ; then
   ((c++))
  else
   fdst=$c ;c=0;break
  fi
 done
for i in ${xr[@]} ; do
  if [ "$i" != "X" ] ; then
   ((c++))
  else
ltdst=$c ;ldst=$(( ${#x[@]} - $(( $ltdst -1 )) ));break
  fi
 done
dst=$(( $ldst - $fdst ))
if [ $dst -gt 3 ] ; then
  for i in $(seq 0 $(( $fdst - 1 )) )
   do
    x[$i]=X
   done
  for i in $(seq $(( $ldst -1 )) $(( ${#x[@]} - 1 )) )
   do
    x[$i]=X
   done
fi
   echo ${x[@]}|sed 's/ //g' >>tmpfileX
done<"$1"
more tmpfileX

# 10  
Old 10-15-2010
Quote:
Originally Posted by patrick87
... I just edit a little bit of my previous post due to my small mistakes.
Do you have any idea to archive it?
As long as the letter (less or equal to 3 letter ) before the first "X" and last "X" is not "X".
I will replace those letter with "X"
...
Not sure what you mean by "idea to archive it" - I don't see anything about archiving in your post. You probably mean "achieve it".

In any case, the script that follows takes care of the case where a line could have a non-X letter on each end.

Code:
$
$
$ cat f25
>Read_1
XXXXXXXXXXABCXXXXXXS
XXXXXXXXXXABCXXXXXRS
XXXXXXXXXXABCXXXXQRS
XXXXXXXXXXABCXXXPQRS
PXXXXXXXXXABCXXXXXXX
PQXXXXXXXXABCXXXXXXX
PQRXXXXXXXABCXXXXXXX
PQRSXXXXXXABCXXXXXXX
PXXXXXXXXXABCXXXXXXS
PXXXXXXXXXABCXXXXXRS
PXXXXXXXXXABCXXXXQRS
PXXXXXXXXXABCXXXPQRS
PQXXXXXXXXABCXXXXXXS
PQXXXXXXXXABCXXXXXRS
PQXXXXXXXXABCXXXXQRS
PQXXXXXXXXABCXXXPQRS
PQRXXXXXXXABCXXXXXXS
PQRXXXXXXXABCXXXXXRS
PQRXXXXXXXABCXXXXQRS
PQRXXXXXXXABCXXXPQRS
PQRSXXXXXXABCXXXXXXS
PQRSXXXXXXABCXXXXXRS
PQRSXXXXXXABCXXXXQRS
PQRSXXXXXXABCXXXPQRS
XXXXXXXXXXABCXXXXXXX
$
$
$ perl -lne 'if    (/^([^X]{1,3})(X.*X)([^X]{1,3})$/) {($a,$b,$c)=($1,$2,$3); $a=~s/./X/g; $c=~s/./X/g; print "$a$b$c"}
             elsif (/^([^X]{1,3})(X.*)/)              {($a,$b)=($1,$2); $a=~s/./X/g; print "$a$b"}
             elsif (/^(.*X)([^X]{1,3})$/)             {($a,$b)=($1,$2); $b=~s/./X/g; print "$a$b"}
             else  {print}
            ' f25
>Read_1
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXPQRS
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
PQRSXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXPQRS
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXPQRS
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXPQRS
PQRSXXXXXXABCXXXXXXX
PQRSXXXXXXABCXXXXXXX
PQRSXXXXXXABCXXXXXXX
PQRSXXXXXXABCXXXPQRS
XXXXXXXXXXABCXXXXXXX
$
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 11  
Old 10-15-2010
Code:
awk '
/^..X/{sub("^..X","XXX")}
/X..$/{sub("X..$","XXX")}
1' file

This User Gave Thanks to Franklin52 For This Post:
# 12  
Old 10-15-2010
Franklin52's idea is much better.
You do not really have to check if the first 3 or last 3 characters are non-X. The result is code brevity.

Code:
$
$ cat f25
>Read_1
XXXXXXXXXXABCXXXXXXS
XXXXXXXXXXABCXXXXXRS
XXXXXXXXXXABCXXXXQRS
XXXXXXXXXXABCXXXPQRS
PXXXXXXXXXABCXXXXXXX
PQXXXXXXXXABCXXXXXXX
PQRXXXXXXXABCXXXXXXX
PQRSXXXXXXABCXXXXXXX
PXXXXXXXXXABCXXXXXXS
PXXXXXXXXXABCXXXXXRS
PXXXXXXXXXABCXXXXQRS
PXXXXXXXXXABCXXXPQRS
PQXXXXXXXXABCXXXXXXS
PQXXXXXXXXABCXXXXXRS
PQXXXXXXXXABCXXXXQRS
PQXXXXXXXXABCXXXPQRS
PQRXXXXXXXABCXXXXXXS
PQRXXXXXXXABCXXXXXRS
PQRXXXXXXXABCXXXXQRS
PQRXXXXXXXABCXXXPQRS
PQRSXXXXXXABCXXXXXXS
PQRSXXXXXXABCXXXXXRS
PQRSXXXXXXABCXXXXQRS
PQRSXXXXXXABCXXXPQRS
XXXXXXXXXXABCXXXXXXX
$
$ # Perl equivalent of Franklin52's script
$ perl -plne 's/^...X/XXXX/; s/X...$/XXXX/' f25
>Read_1
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXPQRS
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
PQRSXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXPQRS
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXPQRS
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXXXXX
XXXXXXXXXXABCXXXPQRS
PQRSXXXXXXABCXXXXXXX
PQRSXXXXXXABCXXXXXXX
PQRSXXXXXXABCXXXXXXX
PQRSXXXXXXABCXXXPQRS
XXXXXXXXXXABCXXXXXXX
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 13  
Old 10-15-2010
Thanks again, Franklin52
Your awk script is wonderful and worked perfectly in my case Smilie

---------- Post updated at 10:35 AM ---------- Previous update was at 10:33 AM ----------

Sorry my mistakes cause your misunderstanding, tyler_durden Smilie
You're right.
It should be "achieve" instead of "archive" Smilie
Thanks a lot for your latest perl command too.
It worked perfect and easy for me to edit the perl command according different situation too.
Thanks again, tyler_durden.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to change the file content using some conditions

Hello, I would like to change the content of the file that has few blocks starts with 10 and ends with ";" File1.txt 10 bonuses D 20 MATCHED 30 UPD COL 40 (SOL=30) 20 NOT MATCHED 30 INS COL 40 (SOL=30) ; 10 bonuses D 20 MATCHED 30 UPD COL 40... (5 Replies)
Discussion started by: Mannu2525
5 Replies

2. Shell Programming and Scripting

Change a file content format using awk

Hi, i have a file input.txt Continent North America Country USA Capital Washington D.C. Country Canada Capital Ottawa Continent South America Country Argentina Capital Buenos Aires Country Brazil Capital Brasília Coutry Colombia Capital Bogotá and i want to get an output.txt ... (3 Replies)
Discussion started by: fastlane3000
3 Replies

3. Shell Programming and Scripting

Help with duplicate data content problem asking

Input file: A_69510335_ASD>aw 1199470 USA A_119571157_C>awe,QWEQE 113932840 USA C_34646666_qwe>TAWTT,G,TT 112736796 UK C_69510335_QW>T 1199470 USA D_70520237_WR>QEE,G 34459863 UK D_71380003_QWR>T 145418226 IK . Desired output: A_69510335_ASD>aw 1199470 USA... (1 Reply)
Discussion started by: perl_beginner
1 Replies

4. Linux

when SCP, does file content change?

Good day to you all, Just want to check here, i know when scping a file, size might change due to space issue. it might sound silly, but does file content get change too? if so, what kind of situation that might be? (1 Reply)
Discussion started by: ahtat99
1 Replies

5. Shell Programming and Scripting

Execution Problems with scan and change file data content

Input file >Read_1 XXXXXXXXXXSDFXXXXXDS ASDRXXXXXTGAGTXXXXXT TGTGATXXXXXAXXXXGXXA . . Desired output file >Read_1 XXXXXXXXXXXXXXXXXXDS ASDRXXXXXTGAGTXXXXXT TGTGATXXXXXXXXXXXXXA . . (5 Replies)
Discussion started by: patrick87
5 Replies

6. Shell Programming and Scripting

Rearrangement of data content problem

Input data: >sample_1 WETYUPVLGK DGGHHHWETY QPERTTGGLO >sample_2 WRRTTOOLLP MKMKNJUTYE DLGLTTOC . . Desired output: >sample_1 WETYUP VLGKDG GHHHWE (8 Replies)
Discussion started by: patrick87
8 Replies

7. Shell Programming and Scripting

Change file content based on data

I have a Transaction File coming into the system. In this file, in all records the relevant data is as follows- Position 1:10 -> Transaction Code Position 252:255 -> 4 digit business code Now based on these 2 fields I have to alter value in Transaction code (Position 1:10)... (6 Replies)
Discussion started by: varunrbs
6 Replies

8. Shell Programming and Scripting

Extract specific content from data and rename its header problem asking

Input file 1: >pattern_5 GAATTCGTTCATGTAGGTTGASDASFGDSGRTYRYGHDGSDFGSDGGDSGSDGSDFGSDF ATTTAATTATGATTCATACGTCATATGTTATTATTCAATCGTATAAAATTATGTGACCTT SDFSDGSDFKSDAFLKJASLFJASKLFSJAKJFHASJKFHASJKFHASJKFHSJAKFHAW >pattern_1 AAGTCTTAAGATATCACCGTCGATTAGGTTTATACAGCTTTTGTGTTATTTAAATTTGAC... (10 Replies)
Discussion started by: patrick87
10 Replies

9. Shell Programming and Scripting

How to change a file's content by row?

Greetings. So the question is basically the same as it's in the title. I'd like to write a program that changes a file by rows. So to clarify it. (i know i shouldn't use code,/code here but i would like to separate it) So for example a text file looks like something like this: Happy... (5 Replies)
Discussion started by: buddhist
5 Replies

10. Shell Programming and Scripting

Need help to change the content for remote located file

Hi All, I have a file that sits on 4 diffrent servers, those servers are diffrent region based and they are authentication protected and that file has a diff port numbers, so when run the script it must ask my login details,region of server and port no for that file once it took from me it... (1 Reply)
Discussion started by: tmarjuna
1 Replies
Login or Register to Ask a Question