Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You say '3-5' and '32-34' which are character numbers. That can be very difficult to nail down, especially if there are any length differences or spaces that push your characters over 1 or 2.
Would it be better to look at it like 'column number'? Like for your example:
Code:
1 2 3 4 5 6 7 8 9
3 abc eelee ref: OAK ARR: ONT abc 0236
Hi, new to the group, and first time poster.
I'm trying to convert "abc" to "def" in a file, but only when found in positions 3,4,5 and 32,33,34.
Example:
I want to convert:
3 abc eelee ref: OAK ARR: ONT abc 0236
to:
3 def eelee ref: OAK ARR: ONT def 0236
but not change:
3 lmn eelee abc: OAK ARR: ONT lmn 0400 abc
I thought sed would be my best option, but having problem figuring out how.
Hi,
While I tend to agree with pan64 that awk may be better, you should post your attempts with sed or awk to show what you have tried.
LQ members are happy to help you, however they are also here as volunteers and further to help you to learn "how to" by your self. Thus it's best to see your earlier attempts to see how you approach a solution and then have members offer refinement. Please post some of the attempts and describe where the outcomes were not correct or what things you wished to do but could not because of your inexperience with either sed or awk, or some other tool.
Thanks all. As I’ve looked more closely at this, I think I have a clear picture of what the original VB code was doing.
If char 3-5 is in the ALL_WIDGETS file
If char 3-5 and 138-140 match
convert both to alt_widget
else
convert char 3-5 to alt_widget
replace char 138-140 with spaces
My problem (in red)is what logic to use to compare char 3-5 with 138-140 on each line to determine whether to convert both occurrences, or whether to convert 3-5, and blank out 138-140 if they differ.
> cat ALL_WIDGETS
abc,hij
def,klm
ghi,nop
> cat convert_widgets
for WIDGETs in `cat ALL_WIDGETS`
do
WIDGET=`echo $WIDGETs | cut -d\, -f1`
grep -q $WIDGET $source_file
if [ $? = 0 ];then
alt_WIDGET=`echo $WIDGETs | cut -d, -f2`
##### If char 3-5 and char 138-140 match, convert both if [ char 138-140 = char 3-5 ];then echo "Converting $WIDGET to $alt_WIDGET @ 3-5"
sed -E "s/^(.{2})$WIDGET/\1$alt_WIDGET /" $source_file > $source_file.tmp
mv $source_file.tmp $source_file
echo "Converting $WIDGET to $alt_WIDGET @ 138-140"
sed -E "s/^(.{137})$WIDGET/\1$alt_WIDGET /" $source_file > $source_file.tmp
mv $source_file.tmp $source_file
else #### Otherwise, convert 3-5, and replace 138-140 with spaces
echo "Converting $WIDGET to $alt_WIDGET @ 3-5"
sed -E "s/^(.{2})$WIDGET/\1$alt_WIDGET /" $source_file > $source_file.tmp
mv $source_file.tmp $source_file
#blank out 138-140
echo "Blanking out $WIDGET @ 138-140"
sed -E "s/^(.{137})$WIDGET/\1 /" $Dest/PLEG > $Dest/PLEG.tmp
mv $source_file.tmp $source_file
fi
fi
done
Thanks Turbocapitalist, unfortunately, my file is not space-delimited , however, every occurrence of 'widget' that I want to convert, is char 3-5 and 138-140 on the lines where 'widget' is found.
Not attempting to write a script for this, however my tact for when I do something like this in sed or just using emacs search and replace, I find a unique property of what I wish to change for my search spec.
From all of your examples, you wish to change all [SPACE]abc[SPACE] to another pattern.
So code for that. Your contrary examples show either abc[COLON] or abc[other characters]
My points there being that you bring up character position, repeatedly, however I see no examples yet showing the simple search won't work. I daresay, (sorry) that you can find or invent more examples to contradict that. But consider (1) if you have more examples, then you really should show all examples now, not incrementally (2) if you're inventing contrary examples because you wish to stick with this adamant restriction, then I really can't help you much except to say that once you find <pattern> you can then evaluate the position to determine if it meets the further criteria for substitution.
My next point is about universal behavior. To whit is my example of either sed or emacs search and replace. Those are universal in that I have to give a search string and a replacement string. If you have an extremely and highly specific edit requirement, it is fine, but for me if I'm fixing one thing once, I do that and move on. If I know I will be fixing something many times over, then I will write re-usable code or script to do so. Therefore allowing for arguments and options and not just coding to a very, highly particular string and range of columns.
rtmistler, here is a before, and after sample of an actual file, and what I am trying to achieve. The values will change, but the changes will always be made to columns 3-5 and 138-140(if applicable)
(note: the red XX represent spaces)
In that example case you can globally replace "BBD" with "BF " and "BRA" with three space characters.
I would two pass that using sed.
Besides examples, how about a more inclusive summary of your requirements. For instance I can see BBD in the header and perhaps that aids you in determining what string to change later in the file. The BRA appears sparingly and therefore what qualifiers would tell someone entering the filename and search strings into a script as arguments to know that they need to specify that string?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.