LinuxQuestions.org
Visit Jeremy's Blog.
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 05-06-2013, 01:19 PM   #16
El Villa
LQ Newbie
 
Registered: May 2013
Posts: 1

Rep: Reputation: Disabled

Hi all!

Im a newbie with bash but i've done my best to work it out.

It skips not existent lines, and shows blank lines (those which only got a number and no-words) as a single number.

It is a bit long because I have needed an array to organise the words by lines, and a second one to add the commas including the following instructions:

- Not to add a comma if the line contains a single word
- Not to add a comma for the last word of a line

Code:
#! /bin/bash
declare -a memo[0]=`sed -n 1p /directory/archive | cut -d " " -f 1`

while read f
do
	declare -a line=($f)
	if [ ${line[0]} -eq ${memo[0]} ] 
	then 
		pos=${#memo[*]}

		for ((a=1;a<${#line[*]};a++))
		do			
			declare -a memo[$pos]=`echo "${line[$a]}"`			
		done
		continue
	fi	
		################################################################
		#all this tricky code down is cause we need commas in output;
		#without commas, just: 
		#echo "${memo[*]}
 
	count=0
	declare -a toscreen=()
	for l in ${memo[*]} 
	do
		if [ $count -gt 0 -a $count -lt `expr ${#memo[*]} - 1` ]
		then		
			toscreen[$count]=$l,
			count=`expr $count + 1`
		else
			toscreen[$count]=$l
			count=`expr $count + 1`
		fi
	done
	declare -a memo=(${line[*]})						
	declare -a line=()
	echo "${toscreen[*]}"
	################################################################

done</directory/archive
################################################################################
#All this down is to output the last line!!
count=0
declare -a toscreen=()
for l in ${memo[*]} 
do
if [ $count -gt 0 -a $count -lt `expr ${#memo[*]} - 1` ]
	then		
		toscreen[$count]=$l,
		count=`expr $count + 1`
	else
		toscreen[$count]=$l
		count=`expr $count + 1`
	fi
done
declare -a memo=(${line[*]})						
declare -a line=()
echo "${toscreen[*]}"
 
  


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
Why does string concatenation in perl result in two lines, not one? markhod Programming 5 01-12-2010 08:00 PM
String concatenation problem in a loop tugce_zehra Linux - Newbie 1 12-06-2008 05:43 PM
Shell string concatenation Quakeboy02 Programming 4 04-26-2007 11:11 AM
string concatenation in c/c++ for a socket program mohtasham1983 Programming 3 02-14-2007 05:42 PM
string concatenation in AWK xanthium Programming 1 04-22-2002 04:41 AM


All times are GMT -5. The time now is 10:39 AM.

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