Using sed '${linenum}d' $filename in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed '${linenum}d' $filename in shell
# 1  
Old 02-10-2012
Using sed '${linenum}d' $filename in shell

Hi Experts,


Please let me know how to below situation:

Looking for:
Code:
sed "${linenum}d" $filename > $filename _Without_7C

Working Command:
Code:
sed "${linenum}d" A020820122200.tis2webGMNA001_GDS_DIDOut.txt > A020820122200.tis2webGMNA001_GDS_DIDOut.txt_Without_7C

appriciate if you can tune below script:
Shell Script
*******************************************
Code:
#!/bin/ksh
##Removing "7C" Occurences of 9th filed from "*.tis2webGMNA001_GDS_DIDOut.txt"##
#Testdir /QIS2FTP/scripts
#set -x
cd /QIS2FTP/scripts
Filename=`ls -1 *.tis2webGMNA001_GDS_DIDOut.txt`
for a in $Filename
do
NonOccur=`cat A020820122200.tis2webGMNA001_GDS_DIDOut.txt |cut -d '|' -f9|grep -vn 7C|awk '{print $1}'|cut -d':' -f1`
Occur=`cat A020820122200.tis2webGMNA001_GDS_DIDOut.txt |cut -d '|' -f9|grep -n 7C|awk '{print $1}'|cut -d':' -f1`
for i in $Occur
#Deleted lines output
do
cat $a|head -$i|tail -1 >> $a_With_7C
done;
#After Deleted lines output (Here i want use sed command)
for j in $NonOccur
do
cat $a |head -$j|tail -1 >> $a_Without_7C 
done;
done;
#set +x

*************************************************

Last edited by methyl; 02-12-2012 at 08:08 PM.. Reason: Please use code tags for code and indent your code, thank you. methyl: removed funny font
# 2  
Old 02-10-2012
can you post some sample input file and the expected output file
# 3  
Old 02-11-2012
Sample Data

Code:
SMaple data

Cat smaplet.txt

LSGWT52X7CS140735|12A_AB17ab1_ac
LSGWT52X7CS140736|22A_AB27ab2_ac
LSGWT52X7CS140737|12A_AB17Cab1_ac
LSGWT52X7CS140738|22A_AB27Cab2_ac
LSGWT52X7CS140739|12A_AB17Cab1_ac
LSGPC54U39F003800|22A_AB27Cab2_ac
LSGPC54U39F003801|12AAB17Cab1ac
LSGPC54U39F003802|22A_AB27Cab2ac
LSGPC54U39F003803|12AAB17Cab1ac
LSGPC54U39F003804|22AAB27Cab2_ac

Moderator's Comments:
Mod Comment Please use code tags!
for your code and data - No colors or fancy fonts!

Last edited by methyl; 02-12-2012 at 08:04 PM.. Reason: removed unusual font
# 4  
Old 02-11-2012
There are just two fields, whereas your code seems mention 9 fields?
# 5  
Old 02-12-2012
Please post sample input and expected output and and a description of the process which turns the input into the output.

You have clearly had a serious attempt to make this process work but it is difficult to follow the intention from the script posted.

The "sed" command has much variation and in your case you need to know if the "sed -i" (inline edit) variation is available.
Please post you Operating System and version. We note that you prefer Korn Shell (ksh).

Last edited by methyl; 02-12-2012 at 08:27 PM..
# 6  
Old 02-13-2012
Sample data and am using AIX 5.2 version

$ cat A020820122200.tis2webhmna001_ADS_AIDOut.txt
/Datastage/Ascential/output/hmna/A020820122200.tis2webhmna001_ADS_AIDOut.txt|0|50|G|20120208110050|20120208110050|0011|LSGPC54U39F003 800|"9071982_008A6D7C_ mn"||US|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|"N"|78_4E|""|_|||||||||||||86DZBSV01009161 0|" "|0_00000000|" mr"|9071986_008A6D72|" mo"|9071983_008A6D6F_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|" mn"|9071982_008A6D6E|"Oê"|55569034_034FEA8A|00|00|00|||||||||00|00
/Datastage/Ascential/output/hmna/A020820122200.tis2webhmna001_ADS_AIDOut.txt|0|50|G|20120208110104|20120208110104|0011|LSGWT52X7CS140 735|"12610782_00C06A22_"| AB|US|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|||||||||||||86YNUYM06074D6P5|""|_| " À¨S"|12626003_00C0A853|" À_¿"|12607423_00C05FBF_|" Àhæ"|12609766_00C068E6|" Àj$"|12610084_00C06A24|" Àhå"|12609765_00C068E5|" Àj5"|12610101_00C06A35|""|_|""|_|" À_Ó"|12607443_00C05FD3|" Àc"|12608385_00C06381||12610082_00C06A22|" ¿Ö."|12572206_00BFD62E||AA|AB|AC|AB|AC|AB|||AA|AB| |
/Datastage/Ascential/output/hmna/A020820122200.tis2webhmna001_ADS_AIDOut.txt|0|50|G|20120208110106|20120208110106|0011|LSGPC54U19F060 660|"9071982_008A6D6E_ mn"||US|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|"N"|78_4E|""|_|||||||||||||86DZBSV01809437 0|" "|0_00000000|" mr"|9071986_008A6D72|" vÄ"|9074372_008A76C4_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|" mn"|9071982_008A6D6E|"Oê"|55569034_034FEA8A|00|00|00|||||||||00|00
/Datastage/Ascential/output/hmna/A020820122200.tis2webhmna001_ADS_AIDOut.txt|0|50|G|20120208105319|20120208105319|0011|LSGGF53X3AH028 322|"12629459_07C0B5D3_ ÀµÓ"| AB|US|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|||||||||||||86YTAJP39219506R|" ÀsG"|12612423_00C07347|" À³^"|12628830_00C0B35E|" w4"|9074484_008A7734_|" ÔÖ"|9032918_0089D4D6|" w<"|9074492_008A773C|" r?"|9073215_008A723F|" yÏ"|9075151_008A79CF|""|_|""|_|" À¤Ï"|12625103_00C0A4CF|" ÔÔ"|9032916_0089D4D4|" ÀµÓ"|12629459_00C0B5D3|" Às"|12612382_00C0731E|AA|AA|AB|AE|AB|AB|AB|||AA|AD| |

---------- Post updated at 10:51 AM ---------- Previous update was at 10:49 AM ----------

/Datastage/Ascential/output/hmna/A020820122200.tis2webhmna001_ADS_AIDOut.txt|0|50|G|20120208110050|20120208110050|0011|LSGPC54U39F003 800|"9071982_008A6D7C_ mn"||US|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|"N"|78_4E|""|_|||||||||||||86DZBSV010091 61 0|" "|0_00000000|" mr"|9071986_008A6D72|" mo"|9071983_008A6D6F_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|""|_|" mn"|9071982_008A6D6E|"Oê"|55569034_034FEA8A|00|00|00|||||||||00|00


is a one line data in above file

Last edited by rajubollas; 02-13-2012 at 01:23 AM.. Reason: on is replaced with One
# 7  
Old 02-13-2012
In shell try this:

Code:
#!/bin/ksh
for file in *.tis2web*IDOut.txt
do
  while read line
  do
    case $line in 
      *7C*) printf "%s\n" "$line" >> "${file}_With_7C" ;;
      *)    printf "%s\n" "$line" >> "${file}_Without_7C" ;;
    esac
  done < "$file"
done

*edit That would no work properly, since we need to test on the 9th field. Come back to that later...

Last edited by Scrutinizer; 02-13-2012 at 01:47 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sed- Replace space in filename by a \

`echo $file | sed 's/ / /g'` Hey guys I want help in converting the spaces in my file names to '\ ' . Example: UK maps --> UK\ maps Could someone please help me. I have tried the following sequences already (none of them work): 1)s/ /\ /g 2)s/ /\\ /g 3)s/ /\\\ /g Can someone... (7 Replies)
Discussion started by: INNSAV1
7 Replies

2. Shell Programming and Scripting

sed to replace pattern with filename

Hi all, I'm trying to replace a pattern/string in about 100 files with the filename using following commands but getting nowhere: for f in *.fa; do sed "s/^>.*/>$f/g" $f > $f_v1.fa; done for f in *.fa; do sed 's/^>.*/>`echo $f`/' > $fa_v1.fa; done Basically I want to change any line... (5 Replies)
Discussion started by: ivpz
5 Replies

3. UNIX for Dummies Questions & Answers

Can someone please explain sed -n '/^$/!{s/<[^>]*>//g;p;}' filename

I came across this sed expression, and it does exactly what I want. However I haven't got the faintest clue how it does it and thus do not feel capable of using it. Can someone please explain how this expression works? (I used it to remove html tags in a html file I was converting to text) ... (3 Replies)
Discussion started by: maximus73
3 Replies

4. UNIX for Dummies Questions & Answers

Adding Filename as column using sed

Hi , Can any one please tell me, how can we add the file name as column using sed. right now we are using the below awk command for adding the file name as column but when we are calling this script from datastage it is deleting the file data..very weird raised a support ticket with datastage.... (2 Replies)
Discussion started by: mora
2 Replies

5. Shell Programming and Scripting

perl linenum script

im working a a perl script for a class and i need to call a file and print it out with line numbers. i need help with this line with what to put in the capitalized words SYNTAX ($LINEEQUALITY OPERATOR<MY_FILE>){ also should i replace MY_FILE with a different syntax or will that work? ... (5 Replies)
Discussion started by: livewire06
5 Replies

6. Shell Programming and Scripting

h=`sed -n '57p' filename`

sed -n '57p' filename My file contains 57 lines ,the above command prints the 57 th line,but i want to assign the result of this to a variable, to compare with another line in a different file suggest me some idea if i directly use this to assign with variable like h=sed -n '57p' filename... (2 Replies)
Discussion started by: ragavendar
2 Replies

7. Shell Programming and Scripting

How to use a dynamic filename with sed?

I have a line that works for static filename cat /directorypath/filename | sed '//d;//d' > filename This approach when used in a script works well. Then i need a list of filenames to give this line. I can get the list into a file by filelist1='ls -m' then use filelist2=${filelist1##ls... (4 Replies)
Discussion started by: ericonanson
4 Replies

8. Shell Programming and Scripting

extract string portion from filename using sed

Hi All, I posted something similar before but I now have a another problem. I have filenames as below TOP_TABIN240_20090323.200903231830 TOP_TABIN235_1_20090323.200903231830 i need to extract the dates as in bold. Using bash v 3.xx Im trying to using the print sed command but... (11 Replies)
Discussion started by: santam
11 Replies

9. Shell Programming and Scripting

insert filename into file using SED (or AWK)

Hi, I would like to insert a file's filename into the first line of that file - for a batch of files. Is this possible using SED? Thanks in advance. (2 Replies)
Discussion started by: USER#5
2 Replies

10. Shell Programming and Scripting

sed / shell - how to use $filename

I've forgotten how to use a filename parameter using sed inside a shell script. What I want to do is replace a string inside a file with the name of the file being processed. I think this should work .. for filename do sed -e "s/xxx/$filename/" ... ... done Thanks! Ps: ... (5 Replies)
Discussion started by: eadie
5 Replies
Login or Register to Ask a Question