Solved

Need a unix command set to list the file name and include the contents of the file on the same line with the file name within a unix shell environment

Posted on 2011-03-11
3
467 Views
Last Modified: 2013-11-22
I have a directory with 100s of small text files that I need to extract the text contents.  

I need to get an output that will list the file name and contents of the file on the same line.

The contents of these text files is 80 ascii characters or less.  

example output format:

{file name} {contents of text file]
{file name} {contents of text file]
{file name} {contents of text file]
{file name} {contents of text file]
{file name} {contents of text file]


I am sure there is a simple unix shell command(s) that will allow this data to be extracted.

Here is a sample list of the files that I need to list and extract the data from:

-rw-rw-r--   1 easm     cog            8 Apr 12  2007 anc01520.dsc
-rw-rw-r--   1 easm     cog           15 Apr 12  2007 anc01521.dsc
-rw-rw-r--   1 easm     cog            4 Apr 12  2007 anc01522.dsc
-rw-rw-r--   1 easm     cog           27 Apr 12  2007 anc01523.dsc
-rw-rw-r--   1 easm     cog           17 Apr 12  2007 anc01524.dsc
-rw-rw-r--   1 easm     cog            9 Apr 12  2007 anc01525.dsc
-rw-rw-r--   1 easm     cog           16 Apr 12  2007 anc01526.dsc
-rw-rw-r--   1 easm     cog           16 Apr 12  2007 anc01527.dsc
-rw-rw-rw-   1 easm     cog           20 Dec  2  2008 anc03501.dsc
-rw-rw-r--   1 easm     cog           25 Apr 12  2007 anc04000.dsc
-rw-rw-r--   1 easm     cog           25 Apr 12  2007 anc04001.dsc
-rw-rw-r--   1 easm     cog           25 Apr 12  2007 anc04002.dsc
-rw-rw-r--   1 easm     cog           25 Apr 12  2007 anc04003.dsc
-rw-rw-r--   1 easm     cog           25 Apr 12  2007 anc04004.dsc
-rwxrwxr-x   1 easm     cog           30 Oct 31  2001 ancXXXXX.dsc
-rwxrwxr-x   1 easm     cog           14 Oct 31  2001 mc21001.dsc
-rwxrwxr-x   1 easm     cog           14 Oct 31  2001 mc21002.dsc
-rwxrwxr-x   1 easm     cog           14 Oct 31  2001 mc21003.dsc
-rwxrwxr-x   1 easm     cog           14 Oct 31  2001 mc21004.dsc
-rwxrwxr-x   1 easm     cog           14 Oct 31  2001 mc21005.dsc
-rwxrwxr-x   1 easm     cog           14 Oct 31  2001 mc21006.dsc
-rwxrwxr-x   1 easm     cog           14 Oct 31  2001 mc21007.dsc
-rwxrwxr-x   1 easm     cog           15 Oct 31  2001 mc21008.dsc
-rwxrwxr-x   1 easm     cog           18 Oct 31  2001 mc21009.dsc
-rwxrwxr-x   1 easm     cog           18 Oct 31  2001 mc21010.dsc
-rw-rw-r--   1 easm     cog           23 Apr 12  2007 mc21012.dsc
-rwxrwxr-x   1 easm     cog           14 Oct 31  2001 mc21018.dsc
-rwxrwxr-x   1 easm     cog           14 Oct 31  2001 mc21019.dsc
-rwxrwxr-x   1 easm     cog           14 Oct 31  2001 mc21020.dsc
-rw-rw-r--   1 easm     cog           23 Apr 12  2007 mc21022.dsc
-rw-rw-r--   1 easm     cog           23 Apr 12  2007 mc21022.dsc.old
-rwxrwxr-x   1 easm     cog           18 Oct 31  2001 mc21023.dsc
0
Comment
Question by:longscuba
3 Comments
 
LVL 10

Expert Comment

by:aboo_s
ID: 35111984
You do have commands to list files in unix but I do not know of any command that will
list the file's content along with it's name!

I think you need to write down the code your self, with c/c++ perhaps!
0
 
LVL 7

Accepted Solution

by:
droyden earned 50 total points
ID: 35111989
if the files do not contain multiple lines then:

 
#!/bin/bash

ls *.dsc | while read i; do 
content=`cat $i`
echo -n "$i $content"
done

Open in new window

0
 

Author Closing Comment

by:longscuba
ID: 35133063
This script had to be modified to run under a ksh environment.

The script was accurate and works well for collecting the necessary data as requested.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Write an app 10 70
MarkLogic 1 79
Best way to split and output to csv in bash 2 64
extracting data from a variable regular expression 2 23
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question