Output various strings to the standard output and stream them individually

Is there a way to redirect each string output to standard out to another command?

// Example file: example.js
#!/usr/bin/env node
process.stdout.write('foo')
process.stdout.write('bar')

After running ./example.js | wc -m, the total character count of both foo and bar combined is 6.

I would like to see the counts separately as 3 and 3. Is there anything specific I need to change within my javascript file or in the command itself?

Answer №1

wc -m is a command that counts the number of characters in its input. It does not have the capability to separate or group by line. This function has no relation to your JS code.

If you want to achieve a different type of counting, you can easily do so with node.js!

Answer №2

In the case that your content is stored in a file with spaces and line breaks, and you need to count the characters in each file:

//example.js    
#!/usr/bin/env node
process.stdout.write('foo')
process.stdout.write('~') // print any delimiter which is not part of your files content
process.stdout.write('bar')

//Use awk to split them and count as usual
./example.js | awk 'BEGIN { RS="~" } {print}' | wc -m
3
3

//Or use awk to remove spaces and get character count
./example.js | awk 'BEGIN { RS="~" } {gsub(" ", "", $0); print length}'

I hope this solution works for your needs.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Express js routing issue ("Page Not Found")

Why do I receive a "Cannot GET /" message when I access my HTTP server at http://localhost:8000/? I am using Express JS for server-side routing and Angular for client-side. Some sources suggest that this error occurs because I haven't set a route for ...

Interacting between frames with jQuery

I have main_page.htm with the following frameset structure: <frameset rows="30,*" frameborder=0 border=0> <frame name="top_frame" src="top.htm"> <frame name="bottom_frame" src="bottom.htm"> </frameset> The content in ...

Tips on using the .map() method to extract data from a JSON response received from a get request and utilizing the content within a specific index to populate table rows

Here is the JSON response representation, https://i.stack.imgur.com/0QWkv.png This is how my project displays it: https://i.stack.imgur.com/LnA5v.png The rendering code snippet is as follows: render() { const { materials } = this.state; ...

What is the best way to execute a function that retrieves data from a MySQL query and then sends it back as a result in Express.js?

How can I refactor my code to efficiently call a function that returns the result of a MySQL query and send it back in an Express.js response? I am attempting to streamline my SQL queries by exporting them into individual functions to eliminate duplicatio ...

How can I modify the default database setting in MongoDB from 'test' to something else?

mongoose.connect(process.env.DATABASE_URL, {useNewUrlParser: true}); const MyModel = mongoose.model(mymodel, new Schema({ name: String })); I have set up a database named 'test' with a collection named 'mymodel'. Could you guide me on ...

Determine the selected radio button

----EDIT---- I am developing a jQuery mobile application and I need to determine which radio button is selected. This is the JavaScript code I'm using: function filter(){ if(document.getElementById('segment1').checked) { aler ...

"Is there a way to retrieve "Lorem ipsum" information from a web service in JSON format

Does anyone know of any sample web services that serve JSON data? I'm looking to practice consuming JSON for testing and learning purposes. I would even be interested in downloading JSON files with images and other content to study offline. Perhaps th ...

The existence of useRef.current is conditional upon its scope, and it may be null in certain

I'm currently working on drawing an image on a canvas using React and Fabric.js. Check out the demo here. In the provided demo, when you click the "Draw image" button, you may notice that the image is not immediately drawn on the canvas as expected. ...

What is the best way to implement locking with Mutex in NodeJS?

Accessing external resources (such as available inventories through an API) is restricted to one thread at a time. The challenges I face include: As the NodeJS server processes requests concurrently, multiple requests may attempt to reserve inventories ...

Error code E11000 was encountered due to a duplicate key error in the myFirstDatabase.posts collection. The issue stems from a duplicate entry in the "text

Looking to gain access to add new posts to my MongoDB database. I have created a module with the following code: const PostSchema = new mongoose.Schema({ title:{ type:String, require: true, unique: true, }, ...

Avoid activating the panel by pressing the button on the expansion header

I'm facing a problem with the delete button on my expansion panel. Instead of just triggering a dialogue, clicking on the delete button also expands the panel. How can I prevent this from happening? https://i.stack.imgur.com/cc4G0.gif <v-expansion ...

React Native's npm start seems to freeze at the "Starting Packager" stage

Having an issue with my react native app created using create-react-native-app. Initially, npm start was running smoothly. However, now it gets stuck at Starting Packager. A couple of days ago, I tried deleting the node_modules folder and reinstalling npm ...

Troubles with rendering output of compiled program when run within a Dockerized environment

My website features a file upload and submit button. When a user uploads a C++ file and submits it, a Docker container is started to compile and run the code. The objective is to showcase the program's output on the web server. Initially, I experience ...

Navigating through relationships in BreezeMongo: Tips and tricks

In my current setup, I have defined two entities: Hospital and Patient with a one-to-many relationship. The metadata for these entities is created as follows: function addHospital() { addType({ name: 'Hospital', ...

Ways to eliminate the Vuetify append-icon from the sequential keyboard navigation

In my Vue.js application using Vuetify, I have implemented a series of password fields using v-text-field with an append-icon to toggle text visibility. Here is the code snippet: <v-text-field v-model="password" :append-icon="show1 ? 'mdi-eye& ...

Ng-repeat seems to be having trouble showing the JSON data

Thank you in advance for any assistance. I have a factory in my application that utilizes a post method to retrieve data from a C# function. Despite successfully receiving the data and logging it to the console, I am facing difficulties in properly display ...

Do you typically define a static variable within a function using `this.temp`?

I am looking to implement a static variable within a function that meets the following criteria: It maintains its value across multiple calls to the function It is only accessible within the scope of that function Below is a basic example of how I am mee ...

Positioning JQuery tooltips

I've been developing a simple tooltip tool (check out the fiddle link below), but I'm encountering some issues with positioning. My goal is to have the tooltip appear centered and above the clicked link, however right now it appears at the top le ...

I would like to terminate the property when processing it on a mobile device

<div class="col-sm-24 embed-responsive"> <iframe width="100%" src="http://www.youtube.com/embed/${item.snippet.resourceId.videoId}"></iframe> </div> .embed-responsive iframe { height: 185px; margin-top: -4%; paddin ...

Calling this.$refs.upload.submit() is not providing the expected response from Element-UI

Currently working with element-ui and attempting to upload a file using the following code: this.$refs.upload.submit(); Is there a way to retrieve the response from this.$refs.upload.submit();? I have attempted the following: .then(response => { t ...