[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
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
Medium Priority
?
488 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
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…
Suggested Courses

872 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