Share your knowledge at the LQ Wiki.
Go Back > Blogs > peonuser
User Name


Rate this Entry

Cut and paste

Posted 04-19-2012 at 04:57 PM by peonuser
Updated 04-19-2012 at 05:32 PM by peonuser (typos)

The "legacy os" people pride themselves in how they support pentium 3 and pentium 4 systems. Funny. We still use linux on pentium I and pentium II machines.

Thanx to all the people who have viewed Over 236,000 views now.

Trying to think of a super simple example of cut and paste for a class. Here is what I came up with:

Play pretend here for a second. You are a new accountant for a company and you need to make a consolidated financial report of many units. (This example is way over simplified, but I think you will get the idea.) You need to get a report out, but you do not have time to re-enter figures, because all the units send their data in ascii or what is known as text files. How can we get the computer to compile the data so it can be easily entered into the spreadsheet? Heres how!

A good prerequisite for this post is at:

Units one, two, and three send their data in, but they are in an ascii file. We want to convert them so they can be easily converted for use in a spreadsheet.

From Unit 1 in unit1:
           Unit 1
Income     5000
Expenses   2000
Data from Unit 2 in unit2:
           Unit 2
Income     45000
Expenses   46000
Data from Unit 3 in unit3:
           Unit 3
Income     18000
Expenses   18000
Lets create a program to compile the data. Nice thing about this is all we have to do to add units that use the same file naming scheme in the directory. No requirements to change the program file. Warning it will kill the old cutitdata, so be sure and back all the files up and and delete ones you do not need. There is of course and easier way, but we will save that for later….
# Get data from unit files and create .CUT files.
for f in unit?
    cut -c 12-25 $f > $newfile

# create tmpfile.
touch cutfile
rm cutfile
touch cutfile

#create Labels for the file.
echo " " > cutitfile
echo Income >> cutitfile
echo Expenses >> cutitfile

# put it all together.
for g  in unit?.CUT
   paste --d , cutitfile  $g >> cutfile
   mv cutfile cutitfile

# uncomment the next line if you want to use sc
# psc -d , < cutitfile >
Make an executable file:

$ chmod +x

Run the program:


See the result file:

$ cat cutitfile

, Unit 1, Unit 2, Unit 3
Income:, 5000, 45000, 18000
Expenses, 2000, 46000, 18000

This is what is known as a comma delimited file which most Spreadsheets can import easily.
Now we can use the spreadsheet to import the data.

If you want to stay completely command line, you can use the program called sc.
You will need to do a conversion though.

$ psc -d , < cutitfile >
$ sc

sc 7.16:  Type '?' for help.
Reading file ""
        A       B       C       D        E         F         G         H     
  0            Unit 1  Unit 2  Unit 3
  1   Income     5000   45000   18000
  2  Expenses    2000   46000   18000
Posted in Uncategorized
Views 758 Comments 0
« Prev     Main     Next »
Total Comments 0




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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration