LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-11-2007, 05:05 PM   #1
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Rep: Reputation: 30
duplicate the line of a text file to the same line


How to duplicate the line of a text file to the same line with a space inserted between them?

e.g.
input text file:
12
34
hello
/usr/xyz

output text file:
12 12
34 34
hello hello
/usr/xyz /usr/xyz
 
Old 01-11-2007, 05:15 PM   #2
FnordPerfect
Member
 
Registered: Dec 2006
Location: Germany
Distribution: Kubuntu (Feisty Fawn), Debian (SID)
Posts: 127

Rep: Reputation: 15
Which language? Mind if I talk bash?

Code:
#!/bin/sh

if [ ! $# -eq 2 ]; then
 echo "Syntax: $0 input_file output_file"
 exit 1
fi

if [ -f "$1" ]; then
  input_file="$1"
else
  echo "Input file \"$1\" does not exist."
  exit 2
fi

output_file="$2"

while read i
do
  echo "$i $i"
done <$input_file >$output_file
Save this in a script named to your likings, eg. insert-space.sh
then type insert-space.sh ./input_text_file.txt ./output_text_file.txt

PS: of course, this will give confusing output, if a line in the input file countains spaces...

Last edited by FnordPerfect; 01-11-2007 at 05:18 PM.
 
Old 01-11-2007, 05:24 PM   #3
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Also using the shell (with sed) you could do the following (the data is in a file called data):
Code:
$ cat data
12
34
hello
/usr/xyz
$ sed -e 's/..*/& &/' data
12 12
34 34
hello hello
/usr/xyz /usr/xyz
 
Old 01-11-2007, 05:27 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could use sed as well.
sed 's/.*/& &/' inputfile >outputfile
 
Old 01-11-2007, 08:27 PM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
cat inputfile | awk '{print $0 " " $0}' > outputfile

jlinkels
 
  


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
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
Parsing text file line by line armandino101 Linux - Newbie 3 12-14-2006 02:43 PM
read line by line form text file in java. spank Programming 1 10-18-2006 02:46 PM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM
Getting a text file line after line in C++ cdog Programming 4 12-31-2005 02:36 PM

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

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