LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-04-2011, 12:15 PM   #1
homexylo
LQ Newbie
 
Registered: Aug 2011
Posts: 3

Rep: Reputation: Disabled
Read a number x from a file and write a character in the xth column of other file


Hey Guys,

I need help with the following situation in bash:

Read a file with sorted numbers.

e.x.
1
2
5
7

Produce other file with characters (e.x "p") in the first line
and in the -associated with the numbers- columns.

e.x.

1-2-3-4-5-6-7
p p - - p - p ( "-" means " " )

As I said I want to read number x from a file and write a character in the xth column of other file, for all the numbers in file.

The produced file is going to be imported in Excel.

Regards
George
 
Old 08-04-2011, 12:27 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,520

Rep: Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944
Quote:
Originally Posted by homexylo View Post
Hey Guys,
I need help with the following situation in bash:

Read a file with sorted numbers.
e.x.
1
2
5
7

Produce other file with characters (e.x "p") in the first line and in the -associated with the numbers- columns.

e.x.
1-2-3-4-5-6-7
p p - - p - p ( "-" means " " )

As I said I want to read number x from a file and write a character in the xth column of other file, for all the numbers in file. The produced file is going to be imported in Excel.
Ok...post what you've written so far, and where you're stuck, and we'll be glad to help. Otherwise, check out the bash scripting guide:
http://tldp.org/LDP/abs/html/
 
Old 08-04-2011, 12:49 PM   #3
homexylo
LQ Newbie
 
Registered: Aug 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you for the fast reply TB0ne!

# produce the file with numbers
awk 'BEGIN {FS="/"} ; {print $2}' $var1$var2$var3$var4.tmp2 > $var1$var2$var3$var4.tmp
# sort the numbers
sort -g $var1$var2$var3$var4.tmp -o $var1$var2$var3$var4.tmp

As you can see I produce the file with the numbers and then sort them.
I tried several thinks with awk and sed and variables..., but i didn't really make anything.
 
Old 08-04-2011, 12:57 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Must this be bash? I have some ideas that are more eloquently done in a different scripting language. (And is this homework, guy?)
 
Old 08-04-2011, 03:39 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A real example would bring to a more proficient help. Anyway, here is an idea in bash:
Code:
 #!/bin/bash
while read num
do
  while [[ $num -gt $((++count)) ]]
  do
    echo -n " "
  done
  echo -n p
done < sorted_file
 
1 members found this post helpful.
Old 08-04-2011, 09:17 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
A less straight forward approach
Code:
awk 'BEGIN{n=1}a=($0 - n){printf "%"a*2"s"," "}{printf "p ";n=++$0}END{printf "\n"}' sorted_file
 
1 members found this post helpful.
Old 08-05-2011, 04:14 AM   #7
homexylo
LQ Newbie
 
Registered: Aug 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you guys! espessially colucix and grail!!
I really appriciate your interest.
 
Old 08-05-2011, 05:28 AM   #8
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
pure unix solution

What about an offset at the beginning? The sequence always starts from 1?

Code:
~$ cat /tmp/ala
2
4
5
8
Run this at the command prompt
Code:
~$ seq 8 | sort -m - ala | uniq -c | cut -d' '  -f7 | tr 12 -p
Cheers

Last edited by igadoter; 08-07-2011 at 04:18 PM. Reason: The another 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
Finding the right file, with only a line and column number Fliggerty Linux - Software 3 08-01-2011 06:27 PM
[SOLVED] Bash, when I read sort write md5 file, the writen file have a space missing peter1234 Linux - General 3 09-21-2010 09:04 AM
Read text file column by column RVF16 Programming 11 05-31-2009 07:16 AM
Write into File from Kernel and read the same file from User space saurabhchokshi Programming 0 05-01-2009 02:26 PM
Find Number of column in a file Swapna173 Linux - Newbie 3 06-04-2008 01:29 AM

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

All times are GMT -5. The time now is 02:49 AM.

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