LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


Reply
  Search this Thread
Old 01-24-2019, 05:48 AM   #1
nab_elf
LQ Newbie
 
Registered: Aug 2013
Posts: 5

Rep: Reputation: Disabled
txt file processing with condition


Hello,
please i would like to process a file as below:

for each line in the file,
if the position 3 to 5 contains 'ABC' , replace in the position from 30 to 33 with 'WXC'


Thank you very much
 
Old 01-24-2019, 05:58 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
sed or perl will do that nicely. What have you tried and where are you stuck?
 
Old 01-24-2019, 06:02 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
I'm sure a bash maven could solve this but I'd lean towards something that has some logic capabilities - awk, perl python.
We're happy to help - what have you tried.
 
Old 01-24-2019, 06:15 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
I would use awk for this task. Roughly:
Code:
/^..ABC/ { ACTION }
where ACTION is based on the sub() function. You can look in up in the awk guide; see the link in my signature.

sed might also work, but I am not sure how to express positions 30 to 33.

EDIT: substr, not sub.

Last edited by berndbausch; 01-24-2019 at 06:17 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] read txt file into an array and make a second txt file zimbot Linux - General 12 09-05-2015 01:39 PM
check Negative egrep condition in an if condition novicunix Programming 5 02-02-2013 12:52 AM
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? tmcguinness Programming 4 02-12-2009 06:38 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
using OR condition in if condition Fond_of_Opensource Linux - Newbie 2 10-20-2006 12:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 08:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration