LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 02-27-2012, 10:16 AM   #1
TigerClaw
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Rep: Reputation: Disabled
Question [bash - sed - awk] Match line with x characters and add string


Hello,

I tried to find a way using the length() function of awk with no success.
I would like to add a string to the beginning or the end of a line that contains less than x character and more than Y characters

Example
Code:
Line:
This line contains less than 100 characters and more than 10
Result:
<string>This line contains less than 100 characters and more than 10
Or
This line contains less than 100 characters and more than 10</string>
Thanks!

Edit: inserted CODE tags

Last edited by TigerClaw; 02-27-2012 at 03:04 PM.
 
Old 02-27-2012, 10:27 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

It might help if you posted your non-working attempts too, so we can help point out where you're going wrong.

Awk can certainly do it, but I'm not going to bother with it right now. I will post a simple bash solution, however.

Code:
x=10
y=100

while read line; do

	ln=${#line}

	if (( ln > x && ln < y )); then
		echo "FOO $line"
	else
		echo "$line"
	fi

done <file.txt
 
Old 02-27-2012, 11:51 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892
Not sure how you could have got stuck??
Code:
awk 'length > 10 && length < 100{print $0,"<string>"}' file
And you can use -v to set variables if required.
 
Old 02-27-2012, 03:06 PM   #4
TigerClaw
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks both solutions works perfectly.

Sorry if my original question was not formulated in the right way, I'll try to do better next time.
 
Old 02-28-2012, 12:22 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892
Please mark as SOLVED once you have a solution
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] sed/awk to print every before line that pattern match niharikaananth Linux - Newbie 10 02-22-2012 10:47 PM
[SOLVED] BASH (grep / sed / awk): find string within a (line) range dragonetti Linux - Newbie 2 11-24-2011 10:16 PM
[SOLVED] Sed, Awk, Perl - Merge lines unless they match a certain string Dsw0002 Programming 5 04-16-2011 05:20 AM
[SOLVED] Need help in replacing set of characters in a specific line using sed or awk bbachu Programming 15 01-03-2011 01:01 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM


All times are GMT -5. The time now is 01:27 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration