LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2022, 01:30 AM   #1
vanKey
LQ Newbie
 
Registered: Jun 2022
Posts: 17

Rep: Reputation: 0
bash: read from character x to y for every line


Hello all,

for the file test.dat, I want to extract the characters at position 7,8 and 9 of each line.
It is important to exactly specify the range from 7 to 9. I can't use $4 to pick it via awk.

content of file test.dat:
Code:
1 2 3 456 789
1 3 4 789 101112
this is how it should like afterwards
Code:
456
789
Has anyone an idea?

Kind regards
vanKey

Last edited by vanKey; 06-17-2022 at 01:34 AM.
 
Old 06-17-2022, 01:35 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752
You mean like this ?
Code:
echo '1 2 3 456 789'|cut -c7,8,9
456
 
Old 06-17-2022, 01:35 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Yes, either AWK or Perl will do that quite easily. Please post what you have tried and where you are stuck. Be sure to use [code] [/code] tags around your work to preserve indentation.
 
Old 06-17-2022, 01:42 AM   #4
vanKey
LQ Newbie
 
Registered: Jun 2022
Posts: 17

Original Poster
Rep: Reputation: 0
What I did is

Code:
awk '{print $4}' test.dat
But I have a far more complex file than this and this is only a minimum example.
What I need is to specify something in awk with reference to the line entry ID's 7-9, that have to be put out.

Kind regards
vanKey

Last edited by vanKey; 06-17-2022 at 01:44 AM.
 
Old 06-17-2022, 01:46 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Quote:
Originally Posted by vanKey View Post
But I have a far more complex file than this and this is only a minimum example.
Thanks. Please provide a more complete example.

Check the various guides periodically as you get closer to your goal. Bruce Barnett's AWK tutorial is quite a good one for that.
 
Old 06-17-2022, 01:59 AM   #6
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
If you always want positions 7-9 then all you need is

cut -c7-9 test.dat >> output-file.dat
 
1 members found this post helpful.
Old 06-17-2022, 03:32 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,145

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
I seem to recall advising you to download and read the gawk doco at the link I provided. Do so.
 
Old 06-17-2022, 04:23 AM   #8
vanKey
LQ Newbie
 
Registered: Jun 2022
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks ArfaSmif, that did it!

Kind regards
 
Old 06-18-2022, 12:17 AM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Why not pure bash, as the thread title suggests?
Code:
while read line; do
    echo "${line:6:3}"
done <file
 
1 members found this post helpful.
Old 06-19-2022, 04:28 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,816

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
Note that the read command defaults to some magic:
skipping initial space, special treatment of backslash.
You can turn the magic off with
Code:
while IFS= read -r line; do
 
Old 06-19-2022, 10:34 AM   #11
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,146
Blog Entries: 6

Rep: Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834
Code:
txt="
1 2 3 456 789
1 3 4 789 101112
a b c sde 12wsd4
3 4 5 67890 33 555
"

while read line; do
    echo "${line:6:3}"
done <<< "$txt"

456
789
sde
678
Bash does not cease to be nifty. No matter how many times you do it.
 
  


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
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
Bash scripting: parsing a text file character-by-character Completely Clueless Programming 13 08-12-2009 09:07 AM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
Removing new line characters on every line execpt first line bioinformatics_guy Linux - Newbie 4 10-21-2008 12:41 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:21 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