Help with text modification and displaying


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with text modification and displaying
# 1  
Old 11-23-2015
Help with text modification and displaying

I have a file storing some text
and another file storing some numbers
I want to display characters other than the specified place of strings


one.txt
Code:
xyz abc 233 skfo 4r443
sfs abc abcd sd fsdf  sdfd 
abc 11 abc 33 abc dsaf

two.txt
Code:
Nt_djd_k='5-6,7-9'
Nt_hh_l='3-6,7-8'

Code:
a=`grep 'Nt_djd_k' two.txt | cut -d "=" -f2 | sed "s/'//g"`

echo $a

cut -c$a one.txt

o/p:
Code:
abc 2
abc a
11 ab

o/p required:
Code:
xyz 33 skfo 4r443
sfs bcd sd fsdf  sdfd
abc bc 33 abc dsaf


Last edited by Don Cragun; 11-23-2015 at 06:09 AM.. Reason: Change ICODE tags to CODE tags and add CODE and ICODE tags.
# 2  
Old 11-23-2015
Please use code tags as required by forum rules!

Does your cut provide the --complement option?
Code:
cut -c$a --complement one.txt
xyz 33 skfo 4r443
sfs bcd sd fsdf sdfd
abc c 33 abc dsaf

This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-24-2015
I am getting this error
Code:
echo "ABCD" | cut -c2 --complement
cut: Not a recognized flag: -
Usage: cut -b List [-n] [File...]
   or: cut -c List [File...]
   or: cut -f List [-d Character] [-s] [File...]

In my system cut doesn't support --complement

Help me with any other option

Last edited by rahulsk; 11-24-2015 at 05:24 AM..
# 4  
Old 11-24-2015
Hello rahulsk,

Following may help you in same, here I am considering that your two.txt file may help more than 2 lines.
Code:
awk 'FNR==NR{match($0,/\=.*/);A[++i]=substr($0,RSTART+2,RLENGTH-3);sub(/\,/,"-",A[i]);next} {if(FNR%2==0){q=FNR>i?i:FNR;{split(A[q], Y,"-");C=substr($0,1,Y[1]-1);if(Y[2]==Y[3]-1){print C OFS substr($0,Y[4]+1)} else {C=C OFS substr(B,Y[2]+1,Y[3]-Y[2]-1);;C=C OFS substr($0,Y[4]);print C;C="";}}}} {if(FNR%2!=0){q=FNR>i?i:FNR;{split(A[q], Y,"-");C=substr($0,1,Y[1]-1);if(Y[2]==Y[3]-1){print C OFS substr($0,Y[4]+1)} else {C=C OFS substr(B,Y[2]+1,Y[3]-Y[2]-1);;C=C OFS substr($0,Y[4]);print C;C="";}}}}' two.txt one.txt

Output will be as follows.
Code:
xyz  33 skfo 4r443
sf abcd sd fsdf  sdfd
ab bc 33 abc dsaf

Also here I want to mention that lets say there are 2 lines(as your shown Input_file) in two.txt file then 1st line rule(to cut the strings from 5-6,7-9) will be applied to all lines in one.txt which are not divided by 2 means all odd lines of one.txt and similarly 2nd line of two.txt rule (to cut the strings from 3-6,7-8) will be applied to all line of one.txt's all even lines, also considering that your two.txt file has only 2 conditions like (5-6,7-9)always, let me know if this helps you.


EDIT: Adding a non-one liner for of solution on same.
Code:
awk 'FNR==NR{
                match($0,/\=.*/);
                A[++i]=substr($0,RSTART+2,RLENGTH-3);
                sub(/\,/,"-",A[i]);
                next
            }
            {
                if(FNR%2==0){
                                q=FNR>i?i:FNR;{ B=$0
                                                split(A[q], Y,"-");
                                                C=substr($0,1,Y[1]-1);
                                                if(Y[2]==Y[3]-1){
                                                                        print C OFS substr($0,Y[4]+1)
                                                                }
                                                else            {     
                                                                        C=C OFS substr(B,Y[2]+1,Y[3]-Y[2]-1);
                                                                        C=C OFS substr(B,Y[4]+1);
                                                                        print C;
                                                                        C="";
                                                                }
                                              }
                            }
             }
             {
                if(FNR%2!=0){
                                q=FNR>i?i:FNR;{
                                                split(A[q], Y,"-");
                                                C=substr($0,1,Y[1]-1);
                                                if(Y[2]==Y[3]-1){
                                                                        print C OFS substr($0,Y[4]+1)
                                                                }
                                                else            {
                                                                        C=C OFS substr(B,Y[2]+1,Y[3]-Y[2]-1);
                                                                        C=C OFS substr($0,Y[4]+1);
                                                                        print C;
                                                                        C="";
                                                                }
                                               }
                            }
             }
    ' two.txt one.txt

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-24-2015 at 07:32 AM.. Reason: Added a non-one liner form of solution now. Changed code a bit
# 5  
Old 11-24-2015
Code:
awk -F, '
$0 ~ SRCH       {gsub (/^.*=\047|\047$/, "")
                 for (i=1; i<=NF; i++)  {n = split ($i, T, "-")
                                         for (m=T[1]; m<=T[n]; m++) D[++j]=m
                                        }
                }
FNR == NR       {next
                }
                {for (i=1; i<=j; i++) $(D[i]) = ""
                }
1
' SRCH="Nt_djd_k" file2 FS="" OFS="" file1
xyz 33 skfo 4r443
sfs bcd sd fsdf  sdfd 
abc c 33 abc dsaf

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Displaying certain text in a msg.

I have a requirement to display a part of an html response that my application gets. The response looks like this: <html><a href='com.aprisma.spectrum.app.sd.client.SDHyperlinkHandler' sdTicketHandle='cr:419900' ocAlarmId='506618ea-f013-102d-02a7-0050569d7aa8'... (3 Replies)
Discussion started by: dlundwall
3 Replies

2. Shell Programming and Scripting

Finding a string and displaying the text before?

Hi folks. I am trying to script a query of a backup server that will display sessions that are "waiting" for a mount... So for, i query my system which returns a process # that is waiting... The output looks like this: 20,984 Backup Storage Pool Primary Pool T950_TAPE_WIN, Copy Pool... (3 Replies)
Discussion started by: Stephan
3 Replies

3. Shell Programming and Scripting

Displaying Result to a Text File

Hi; I am scripting in Shell and i want to write output (on screen) to a text file? ... | tee gcsw/output.txt doesnot work? :(:( (6 Replies)
Discussion started by: gc_sw
6 Replies

4. UNIX for Dummies Questions & Answers

Displaying text from a MySQL table which can be modified

Hi am creating a website for my third year at uni, am trying to create a website where the client can update the content of the site themselves, i will have a news page and i want the content to be draw from my database and displayed on the front end of the site i also want to have an admin side... (3 Replies)
Discussion started by: richeyrich86
3 Replies

5. Shell Programming and Scripting

Text string modification

My shell needs to take a 10 digit number and output it in the forum nnn-nnn-nnnn I considered using the fold command but that won't help too much. (11 Replies)
Discussion started by: computethis
11 Replies

6. UNIX for Dummies Questions & Answers

Displaying Text

Hi guys! I am very much new to UNIX...and I was just wondering on how you would display this text format in UNIX with only just one command : Text 1 Text 2 Text 3 Texts are in aligned vertically in such format...Thanks for your help :) (8 Replies)
Discussion started by: Knowledge_Xfer
8 Replies

7. HP-UX

Pid X killed due to text modification or page I/O error

Hello everybody, I have a HP-UX B.11.11. I had one disk and added one new. When trying to configure the second disk Not Using the Logical Volume Manager(from SAM) I have this error: Pid X killed due to text modification or page I/O error I tryed to add another partion on the first disk,... (5 Replies)
Discussion started by: savus
5 Replies

8. Shell Programming and Scripting

Displaying the Last Modification Time of a specific file

How can I get and display the last modification time of a file? in scripting or specifically using Batch file I want this info for me to determine whether an image has been edited or not by using the last modification time and compare it to our stored date of modification. can somebody help... (5 Replies)
Discussion started by: jaque18
5 Replies

9. Shell Programming and Scripting

SED for text file modification

Hi all, i have a text file something like that FLAG DATE(YYYYMMDD) TIME(HHMMSS) FLAG1 DATE20060101 141216 FLAG1 DATE20070101 141216 FLAG2 DATE20060102 140010 FLAG2 DATE20060103 101212 FLAG1 ... (6 Replies)
Discussion started by: gfhgfnhhn
6 Replies

10. Programming

Text Modification and page I/O error

Hi!, while launching my C application on HP-UX 10.2 workstation, I get the following error message sometime: Pid 9951 killed due to text modification or page I/O error Could someone tell me what does this error means?? and how is this caused? Thanx in Advance, JP (4 Replies)
Discussion started by: jyotipg
4 Replies
Login or Register to Ask a Question